文章目录
主要讲述对list进行排序的几种方式以及找出list中的最大值和最小值
一、先来个简单的开胃菜—最简单的排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @业务描述:
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 11:18
* @copyright (c) ratelfu 版权所有
*/
public class ListSort {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(new Integer(5));
list.add(new Integer(13));
list.add(new Integer(4));
list.add(new Integer(9));
Collections.sort(list);
System.out.println(list.toString());
}
}
程序运行结果:
[4, 5, 9, 13],这种简单的排序直接按照自然顺序进行升序排列。
二、list参数类型相对复杂的排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @业务描述:
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 10:43
* @copyright (c) ratelfu 版权所有
*/
public class UserMax {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
list.add(new User("张三", 5));
list.add(new User("李四", 30));
list.add(new User("王五", 19));
list.add(new User("陈十七", 17)); // 陈十七永远十七岁
Collections.sort(list); // 按年龄排序
System.out.println(list.toString());
}
}
class User implements Comparable<User> {
private String name; //姓名
private int age; // 年龄
public User(String name, int age) {
this.name = name;
this.age = age;
}
// getter && setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(User user) { //重写Comparable接口的compareTo方法,
return this.age - user.getAge();// 根据年龄升序排列,降序修改相减顺序即可
}
}
程序运行结果:根据年龄升序排列
[User [name=张三, age=5], User [name=陈十七, age=17], User [name=王五, age=19], User [name=李四, age=30]]
三、匿名内部类实现排序
匿名内部类实现排序能本次方法范围内覆盖掉原有的类排序规则。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @业务描述: 匿名内部实现Comparable接口并找出最大值和最小值
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 10:43
* @copyright (c) ratelfu 版权所有
*/
public class UserAnonymous {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
list.add(new User("张三", 5));
list.add(new User("李四", 30));
list.add(new User("王五", 19));
list.add(new User("陈十七", 17)); // 陈十七永远十七岁
Collections.sort(list, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
int diff = u1.getAge() - u2.getAge();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0; //相等为0
}
}); // 按年龄排序
System.out.println(list.toString());
}
}
运行结果:[User [name=张三, age=5], User [name=陈十七, age=17], User [name=王五, age=19], User [name=李四, age=30]]
总结:以上就是list排序经常用的几种方法,前两种方法都是通过实体实现Comparable接口并重写compareTo方法,第一种方式之所以没实现接口并实现compareTo方法,通过查看Interger类源码可以看到,java已经帮我们实现好了,所以不用再次写
最后一种方法通过匿名内部类实现Comparator接口实现compare方法,通过自定义比较器来实现排序。
四、常规方法找出list中的最大值以及最小值
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @业务描述:
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 11:18
* @copyright (c) ratelfu 版权所有
*/
public class ListSort {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(new Integer(5));
list.add(new Integer(13));
list.add(new Integer(4));
list.add(new Integer(9));
Collections.sort(list);
System.out.println(list.toString());
Integer max = list.get(0);
Integer min = list.get(0);
for (int i = 0; i < list.size(); i++) {
if (min > list.get(i)) {
min = list.get(i);
}
if (max < list.get(i)) {
max = list.get(i);
}
}
System.out.println("最大值" + max);
System.out.println("最小值" + min);
}
}
五、实现Comparable接口并找出最大值和最小值
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @业务描述:常规方法实现Comparable接口并找出最大值和最小值
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 10:43
* @copyright (c) ratelfu 版权所有
*/
public class UserMaxMin {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
list.add(new User("张三", 5));
list.add(new User("李四", 30));
list.add(new User("王五", 19));
list.add(new User("陈十七", 17)); // 陈十七永远十七岁
User max = Collections.max(list);
System.out.println("最大值"+max.toString());
User min = Collections.min(list);
System.out.println("最小值"+min.toString());
}
}
class User implements Comparable<User> {
private String name; //姓名
private int age; // 年龄
public User(String name, int age) {
this.name = name;
this.age = age;
}
// getter && setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(User user) { //重写Comparable接口的compareTo方法,
return this.age - user.getAge();// 根据年龄升序排列,降序修改相减顺序即可
}
}
六、匿名内部类实现查找list中的最大值和最小值
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @业务描述: 匿名内部实现Comparable接口并找出最大值和最小值
* @package_name: PACKAGE_NAME
* @project_name: calculator-factory
* @author: ratelfu@qq.com
* @create_time: 2019-05-14 10:43
* @copyright (c) ratelfu 版权所有
*/
public class UserAnonymous {
public static void main(String[] args) {
List<User> list = new ArrayList<User>();
list.add(new User("张三", 5));
list.add(new User("李四", 30));
list.add(new User("王五", 19));
list.add(new User("陈十七", 17)); // 陈十七永远十七岁
User max = Collections.max(list, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
int diff = u1.getAge() - u2.getAge();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0; //相等为0
}
});// 按年龄排序
System.out.println("最大值"+max.toString());
User min = Collections.min(list, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
int diff = u1.getAge() - u2.getAge();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0; //相等为0
}
});// 按年龄排序
System.out.println("最小值"+min.toString());
}
}