java8 list集合内排序问题
list是java开发最常用的容器之一,有时需要对获取的list按照一定的规则排序。在java8之前我们一般会想到的两种方法是:(1)比较器,即实现Comparator接口,(2)自比较,实现Comparable接口。java8以后使用lamda表达式比较可以大大简化list集合排序的代码实现。下面我分别用这三种方法对list进行排序:
lamda表达式写法:
创建model类
package xcwlkj;
public class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
//main方法调试
package xcwlkj;
import java.util.ArrayList;
import java.util.List;
public class Test3 {
public static void main(String[] args) throws InterruptedException, ClassNotFoundException {
List stuList=new ArrayList();
stuList.add(new Student("xiaoming", 12));
stuList.add(new Student("xiaohua", 15));
stuList.add(new Student("xiaoli", 11));
/**lamda表达式内部调用compareTo方法,该方法只能比较字符串,排序字段为int需先转化*/
stuList.sort((h1, h2) -> String.valueOf(h1.getAge()).compareTo(String.valueOf(h2.getAge())));
stuList.forEach(item->System.out.println(item.getName()+",年龄:"+item.getAge()));
}
}
运行结果:
实现Comparable接口写法:
创建model类,在student类中实现Comparable接口,重写compareTo方法。
package xcwlkj;
public class Student implements Comparable{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 int compareTo(Student student) {
return this.age-student.age;
}
}
//main方法调试:
package xcwlkj;
import java.util.ArrayList;
import java.util.List;
public class Test3 {
public static void main(String[] args) throws InterruptedException, ClassNotFoundException {
List stuList=new ArrayList();
stuList.add(new Student("xiaoming", 12));
stuList.add(new Student("xiaohua", 15));
stuList.add(new Student("xiaoli", 11));
Collections.sort(stuList);
for (int i = 0; i
}
}
运行结果:
实现Comparator接口写法:
创建model类
package xcwlkj;
public class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
//main方法调试:
package xcwlkj;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test3 {
public static void main(String[] args) throws InterruptedException, ClassNotFoundException {
List stuList=new ArrayList();
stuList.add(new Student("xiaoming", 12));
stuList.add(new Student("xiaohua", 15));
stuList.add(new Student("xiaoli", 11));
/**创建比较器实例*/
NewComparator comparator=new Test3().new NewComparator();
Collections.sort(stuList, comparator);
for (int i = 0; i
}
//比较器
class NewComparator implements Comparator{
@Override
public int compare(Student student1, Student student2) {
return student1.getAge()-student2.getAge();
}
}
}
运行结果:
三种方法都能实现对集合按照一定规则排序,java8的lamda表达式相较于之前的方法代码更简洁,不需要在model类实现comparable接口,也不需要构造比较器。直接在表达式内调用compareTo方法即可。