java8 使用lamda表达式对list集合进行排序

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<Student> stuList=new ArrayList<Student>();
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<Student> stuList=new ArrayList<Student>();
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 <stuList.size() ; i++) {
	System.out.println(stuList.get(i).getName()+",年龄:"+stuList.get(i).getAge());
}

}
}
运行结果:
在这里插入图片描述
实现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<Student> stuList=new ArrayList<Student>();
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 <stuList.size() ; i++) {
	System.out.println(stuList.get(i).getName()+",年龄:"+stuList.get(i).getAge());
}

}
//比较器
class NewComparator implements Comparator{

@Override
public int compare(Student student1, Student student2) {

	return student1.getAge()-student2.getAge();
}

}
}
运行结果:
在这里插入图片描述
三种方法都能实现对集合按照一定规则排序,java8的lamda表达式相较于之前的方法代码更简洁,不需要在model类实现comparable接口,也不需要构造比较器。直接在表达式内调用compareTo方法即可。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值