Python 官方文档:入门教程 => 点击学习
目录前言1、定义一个基础类2、使用Comparator排序3、使用lambda排序4、使用Comparator的comparing方法排序5、自定义比对方法6、使用stream排序7
在日常开发中,对数据排序是非常常见的一种需求,一般通过如下两种方式:
下面聊聊通过Java中的lambda和stream实现在内存中对数据进行排序。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String name;
private int age;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
@Test
void test() {
List<Student> students = Lists.newArrayList(
new Student("caocao", 21),
new Student("sunquan", 20)
);
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
Assertions.assertEquals(students.get(0), new Student("caocao", 21));
}
在定义的Comparator中使用name字段排序,string类型的排序是通过ASCII码顺序进行判断。
@Test
void test() {
List<Student> students = Lists.newArrayList(
new Student("caocao", 21),
new Student("sunquan", 20)
);
students.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
Assertions.assertEquals(students.get(0), new Student("caocao", 21));
}
将内部类Comparator替换为lambda表达式,使代码更简洁。
@Test
void test() {
List<Student> students = Lists.newArrayList(
new Student("caocao", 21),
new Student("sunquan", 20)
);
students.sort(Comparator.comparing(Student::getName));
Assertions.assertEquals(students.get(0), new Student("caocao", 21));
}
在Student类中自定义比对方法
public static int compareByNameThenAge(Student s1, Student s2) {
if (s1.name.equals(s2.name)) {
return Integer.compare(s1.age, s2.age);
} else {
return s1.name.compareTo(s2.name);
}
}
先比对name,再比对age
@Test
void test() {
List<Student> students = Lists.newArrayList(
new Student("caocao", 21),
new Student("sunquan", 20)
);
students.sort((o1,o2) -> Student.compareByNameThenAge(o1,o2));
Assertions.assertEquals(students.get(0), new Student("caocao", 21));
}
在流式计算时进行排序
@Test
void test() {
List<Student> students = Lists.newArrayList(
new Student("caocao", 21),
new Student("sunquan", 20)
);
List<Student> result = students.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
}).collect(Collectors.toList());
Assertions.assertEquals(result.get(0), new Student("caocao", 21));
}
若列表中元素是null或列表中元素参与排序的字段是null,会出现NullPointException异常,即 NPE,简单演示一下:
@Test
void sortedNullGotNPE() {
List<Student> students = Lists.newArrayList(
null,
new Student("liubei", 12)
);
students.sort(Comparator.comparing(Student::getName));
}
修改为:
@Test
void sortedNullGotNPE() {
List<Student> students = Lists.newArrayList(
null,
new Student("liubei", 12)
);
//students.sort(Comparator.comparing(Student::getName));
Assertions.assertThrows(NullPointerException.class,
() -> students.sort(Comparator.comparing(Student::getName)));
}
到此这篇关于Java中的lambda和stream实现排序的文章就介绍到这了,更多相关Java lambda和stream 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java中的lambda和stream实现排序
本文链接: https://www.lsjlt.com/news/168077.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0