Java 進(jìn)階使用 Lambda 表達(dá)式實(shí)現(xiàn)超強(qiáng)的排序功能
我們?cè)谙到y(tǒng)開(kāi)發(fā)過(guò)程中,對(duì)數(shù)據(jù)排序是很常見(jiàn)的場(chǎng)景。一般來(lái)說(shuō),我們可以采用兩種方式:
- 借助存儲(chǔ)系統(tǒng)(SQL、NoSQL、NewSQL 都支持)的排序功能,查詢的結(jié)果即是排好序的結(jié)果
- 查詢結(jié)果為無(wú)序數(shù)據(jù),在內(nèi)存中排序。
今天要說(shuō)的是第二種排序方式,在內(nèi)存中實(shí)現(xiàn)數(shù)據(jù)排序。
首先,我們定義一個(gè)基礎(chǔ)類(lèi),后面我們將根據(jù)這個(gè)基礎(chǔ)類(lèi)演示如何在內(nèi)存中排序。
@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);
}
}
基于Comparator排序
在 Java8 之前,我們都是通過(guò)實(shí)現(xiàn)Comparator接口完成排序,比如:
new Comparator<Student>() {
@Override
public int compare(Student h1, Student h2) {
return h1.getName().compareTo(h2.getName());
}
};
這里展示的是匿名內(nèi)部類(lèi)的定義,如果是通用的對(duì)比邏輯,可以直接定義一個(gè)實(shí)現(xiàn)類(lèi)。使用起來(lái)也比較簡(jiǎn)單,如下就是應(yīng)用:
@Test
void baseSortedOrigin() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student h1, Student h2) {
return h1.getName().compareTo(h2.getName());
}
});
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
這里使用了 Junit5 實(shí)現(xiàn)單元測(cè)試,用來(lái)驗(yàn)證邏輯非常適合。
因?yàn)槎x的Comparator是使用name字段排序,在 Java 中,String類(lèi)型的排序是通過(guò)單字符的 ASCII 碼順序判斷的,J排在T的前面,所以Jerry排在第一個(gè)。
使用 Lambda 表達(dá)式替換Comparator匿名內(nèi)部類(lèi)
使用過(guò) Java8 的 Lamdba 的應(yīng)該知道,匿名內(nèi)部類(lèi)可以簡(jiǎn)化為 Lambda 表達(dá)式為:
Collections.sort(students, (Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));
在 Java8 中,List類(lèi)中增加了sort方法,所以Collections.sort可以直接替換為:
students.sort((Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));
根據(jù) Java8 中 Lambda 的類(lèi)型推斷,我們可以將指定的Student類(lèi)型簡(jiǎn)寫(xiě):
students.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
至此,我們整段排序邏輯可以簡(jiǎn)化為:
@Test
void baseSortedLambdaWithInferring() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
students.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
通過(guò)靜態(tài)方法抽取公共的 Lambda 表達(dá)式
我們可以在Student中定義一個(gè)靜態(tài)方法:
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);
}
}
這個(gè)方法需要返回一個(gè)int類(lèi)型參數(shù),在 Java8 中,我們可以在 Lambda 中使用該方法:
@Test
void sortedUsingStaticMethod() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
students.sort(Student::compareByNameThenAge);
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
借助Comparator的comparing方法
在 Java8 中,Comparator類(lèi)新增了comparing方法,可以將傳遞的Function參數(shù)作為比較元素,比如:
@Test
void sortedUsingComparator() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
students.sort(Comparator.comparing(Student::getName));
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
多條件排序
我們?cè)陟o態(tài)方法一節(jié)中展示了多條件排序,還可以在Comparator匿名內(nèi)部類(lèi)中實(shí)現(xiàn)多條件邏輯:
@Test
void sortedMultiCondition() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12),
new Student("Jerry", 13)
);
students.sort((s1, s2) -> {
if (s1.getName().equals(s2.getName())) {
return Integer.compare(s1.getAge(), s2.getAge());
} else {
return s1.getName().compareTo(s2.getName());
}
});
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
從邏輯來(lái)看,多條件排序就是先判斷第一級(jí)條件,如果相等,再判斷第二級(jí)條件,依次類(lèi)推。在 Java8 中可以使用comparing和一系列thenComparing表示多級(jí)條件判斷,上面的邏輯可以簡(jiǎn)化為:
@Test
void sortedMultiConditionUsingComparator() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12),
new Student("Jerry", 13)
);
students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
這里的thenComparing方法是可以有多個(gè)的,用于表示多級(jí)條件判斷,這也是函數(shù)式編程的方便之處。
在Stream中進(jìn)行排序
Java8 中,不但引入了 Lambda 表達(dá)式,還引入了一個(gè)全新的流式 API:Stream API,其中也有sorted方法用于流式計(jì)算時(shí)排序元素,可以傳入Comparator實(shí)現(xiàn)排序邏輯:
@Test
void streamSorted() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final Comparator<Student> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
final List<Student> sortedStudents = students.stream()
.sorted(comparator)
.collect(Collectors.toList());
Assertions.assertEquals(sortedStudents.get(0), new Student("Jerry", 12));
}
同樣的,我們可以通過(guò) Lambda 簡(jiǎn)化書(shū)寫(xiě):
@Test
void streamSortedUsingComparator() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final Comparator<Student> comparator = Comparator.comparing(Student::getName);
final List<Student> sortedStudents = students.stream()
.sorted(comparator)
.collect(Collectors.toList());
Assertions.assertEquals(sortedStudents.get(0), new Student("Jerry", 12));
}
倒序排列
調(diào)轉(zhuǎn)排序判斷
排序就是根據(jù)compareTo方法返回的值判斷順序,如果想要倒序排列,只要將返回值取返即可:
@Test
void sortedReverseUsingComparator2() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final Comparator<Student> comparator = (h1, h2) -> h2.getName().compareTo(h1.getName());
students.sort(comparator);
Assertions.assertEquals(students.get(0), new Student("Tom", 10));
}
可以看到,正序排列的時(shí)候,我們是h1.getName().compareTo(h2.getName()),這里我們直接倒轉(zhuǎn)過(guò)來(lái),使用的是h2.getName().compareTo(h1.getName()),也就達(dá)到了取反的效果。在 Java 的Collections中定義了一個(gè)java.util.Collections.ReverseComparator內(nèi)部私有類(lèi),就是通過(guò)這種方式實(shí)現(xiàn)元素反轉(zhuǎn)。
借助Comparator的reversed方法倒序
在 Java8 中新增了reversed方法實(shí)現(xiàn)倒序排列,用起來(lái)也是很簡(jiǎn)單:
@Test
void sortedReverseUsingComparator() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final Comparator<Student> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
students.sort(comparator.reversed());
Assertions.assertEquals(students.get(0), new Student("Tom", 10));
}
在Comparator.comparing中定義排序反轉(zhuǎn)
comparing方法還有一個(gè)重載方法,java.util.Comparator#comparing(java.util.function.Function<? super T,? extends U>, java.util.Comparator<? super U>),第二個(gè)參數(shù)就可以傳入Comparator.reverseOrder(),可以實(shí)現(xiàn)倒序:
@Test
void sortedUsingComparatorReverse() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
students.sort(Comparator.comparing(Student::getName, Comparator.reverseOrder()));
Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}
在Stream中定義排序反轉(zhuǎn)
在Stream中的操作與直接列表排序類(lèi)似,可以反轉(zhuǎn)Comparator定義,也可以使用Comparator.reverseOrder()反轉(zhuǎn)。實(shí)現(xiàn)如下:
@Test
void streamReverseSorted() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final Comparator<Student> comparator = (h1, h2) -> h2.getName().compareTo(h1.getName());
final List<Student> sortedStudents = students.stream()
.sorted(comparator)
.collect(Collectors.toList());
Assertions.assertEquals(sortedStudents.get(0), new Student("Tom", 10));
}
@Test
void streamReverseSortedUsingComparator() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
final List<Student> sortedStudents = students.stream()
.sorted(Comparator.comparing(Student::getName, Comparator.reverseOrder()))
.collect(Collectors.toList());
Assertions.assertEquals(sortedStudents.get(0), new Student("Tom", 10));
}
null 值的判斷
前面的例子中都是有值元素排序,能夠覆蓋大部分場(chǎng)景,但有時(shí)候我們還是會(huì)碰到元素中存在null的情況:
- 列表中的元素是 null
- 列表中的元素參與排序條件的字段是 null
如果還是使用前面的那些實(shí)現(xiàn),我們會(huì)碰到NullPointException異常,即 NPE,簡(jiǎn)單演示一下:
@Test
void sortedNullGotNPE() {
final List<Student> students = Lists.newArrayList(
null,
new Student("Snoopy", 12),
null
);
Assertions.assertThrows(NullPointerException.class,
() -> students.sort(Comparator.comparing(Student::getName)));
}
所以,我們需要考慮這些場(chǎng)景。
元素是 null 的笨拙實(shí)現(xiàn)
最先想到的就是判空:
@Test
void sortedNullNoNPE() {
final List<Student> students = Lists.newArrayList(
null,
new Student("Snoopy", 12),
null
);
students.sort((s1, s2) -> {
if (s1 == null) {
return s2 == null ? 0 : 1;
} else if (s2 == null) {
return -1;
}
return s1.getName().compareTo(s2.getName());
});
Assertions.assertNotNull(students.get(0));
Assertions.assertNull(students.get(1));
Assertions.assertNull(students.get(2));
}
我們可以將判空的邏輯抽取出一個(gè)Comparator,通過(guò)組合方式實(shí)現(xiàn):
class NullComparator<T> implements Comparator<T> {
private final Comparator<T> real;
NullComparator(Comparator<? super T> real) {
this.real = (Comparator<T>) real;
}
@Override
public int compare(T a, T b) {
if (a == null) {
return (b == null) ? 0 : 1;
} else if (b == null) {
return -1;
} else {
return (real == null) ? 0 : real.compare(a, b);
}
}
}
在 Java8 中已經(jīng)為我們準(zhǔn)備了這個(gè)實(shí)現(xiàn)。
使用Comparator.nullsLast和Comparator.nullsFirst
使用Comparator.nullsLast實(shí)現(xiàn)null在結(jié)尾:
@Test
void sortedNullLast() {
final List<Student> students = Lists.newArrayList(
null,
new Student("Snoopy", 12),
null
);
students.sort(Comparator.nullsLast(Comparator.comparing(Student::getName)));
Assertions.assertNotNull(students.get(0));
Assertions.assertNull(students.get(1));
Assertions.assertNull(students.get(2));
}
使用Comparator.nullsFirst實(shí)現(xiàn)null在開(kāi)頭:
@Test
void sortedNullFirst() {
final List<Student> students = Lists.newArrayList(
null,
new Student("Snoopy", 12),
null
);
students.sort(Comparator.nullsFirst(Comparator.comparing(Student::getName)));
Assertions.assertNull(students.get(0));
Assertions.assertNull(students.get(1));
Assertions.assertNotNull(students.get(2));
}
是不是很簡(jiǎn)單,接下來(lái)我們看下如何實(shí)現(xiàn)排序條件的字段是 null 的邏輯。
排序條件的字段是 null
這個(gè)就是借助Comparator的組合了,就像是套娃實(shí)現(xiàn)了,需要使用兩次Comparator.nullsLast,這里列出實(shí)現(xiàn):
@Test
void sortedNullFieldLast() {
final List<Student> students = Lists.newArrayList(
new Student(null, 10),
new Student("Snoopy", 12),
null
);
final Comparator<Student> nullsLast = Comparator.nullsLast(
Comparator.nullsLast( // 1
Comparator.comparing(
Student::getName,
Comparator.nullsLast( // 2
Comparator.naturalOrder() // 3
)
)
)
);
students.sort(nullsLast);
Assertions.assertEquals(students.get(0), new Student("Snoopy", 12));
Assertions.assertEquals(students.get(1), new Student(null, 10));
Assertions.assertNull(students.get(2));
}
代碼邏輯如下:
- 代碼 1 是第一層 null-safe 邏輯,用于判斷元素是否為 null;
- 代碼 2 是第二層 null-safe 邏輯,用于判斷元素的條件字段是否為 null;
- 代碼 3 是條件
Comparator,這里使用了Comparator.naturalOrder(),是因?yàn)槭褂昧?code>String排序,也可以寫(xiě)為String::compareTo。如果是復(fù)雜判斷,可以定義一個(gè)更加復(fù)雜的Comparator,組合模式就是這么好用,一層不夠再套一層。
文末總結(jié)
本文演示了使用 Java8 中使用 Lambda 表達(dá)式實(shí)現(xiàn)各種排序邏輯,新增的語(yǔ)法糖真香。
到此這篇關(guān)于Java 進(jìn)階使用 Lambda 表達(dá)式實(shí)現(xiàn)超強(qiáng)的排序功能的文章就介紹到這了,更多相關(guān)java Lambda 表達(dá)式排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java類(lèi)實(shí)現(xiàn)日期的時(shí)間差的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java類(lèi)實(shí)現(xiàn)日期的時(shí)間差的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
Java基于HttpClient實(shí)現(xiàn)RPC的示例
HttpClient可以實(shí)現(xiàn)使用Java代碼完成標(biāo)準(zhǔn)HTTP請(qǐng)求及響應(yīng)。本文主要介紹了Java基于HttpClient實(shí)現(xiàn)RPC,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Java8之lambda最佳實(shí)踐_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在8 里面Lambda是最火的主題,不僅僅是因?yàn)檎Z(yǔ)法的改變,更重要的是帶來(lái)了函數(shù)式編程的思想,我覺(jué)得優(yōu)秀的程序員,有必要學(xué)習(xí)一下函數(shù)式編程的思想以開(kāi)闊思路2017-06-06
Java List的remove()方法陷阱以及性能優(yōu)化
Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下2021-10-10
JPA使用樂(lè)觀鎖應(yīng)對(duì)高并發(fā)方式
這篇文章主要介紹了JPA使用樂(lè)觀鎖應(yīng)對(duì)高并發(fā)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

