Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的實(shí)現(xiàn)代碼
基于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)部類的定義,如果是通用的對(duì)比邏輯,可以直接定義一個(gè)實(shí)現(xiàn)類。使用起來(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類型的排序是通過(guò)單字符的 ASCII 碼順序判斷的,J排在T的前面,所以Jerry排在第一個(gè)。
使用 Lambda 表達(dá)式替換Comparator匿名內(nèi)部類
使用過(guò) Java8 的 Lamdba 的應(yīng)該知道,匿名內(nèi)部類可以簡(jiǎn)化為 Lambda 表達(dá)式為:
Collections.sort(students, (Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));
在 Java8 中,List類中增加了sort方法,所以Collections.sort可以直接替換為:
students.sort((Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));
根據(jù) Java8 中 Lambda 的類型推斷,可以將指定的Student類型簡(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類型參數(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類新增了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));
}多條件排序
在靜態(tài)方法一節(jié)中展示了多條件排序,還可以在Comparator匿名內(nè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í)條件,依次類推。在 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)部私有類,就是通過(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)
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import java.util.Comparator;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return 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 && name.equals(student.name);
}
@Override
public int hashCode() {
return name.hashCode() + age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ComparatorTest {
@Test
void sortedUsingComparatorReverse() {
final List<Student> students = Lists.newArrayList(
new Student("Tom", 10),
new Student("Jerry", 12)
);
// 使用Comparator.comparing的重載方法,傳入Comparator.reverseOrder()實(shí)現(xiàn)倒序
students.sort(Comparator.comparing(Student::getName, Comparator.reverseOrder()));
// 驗(yàn)證排序結(jié)果
assertEquals(students.get(0), new Student("Jerry", 12));
}
}在Stream中定義排序反轉(zhuǎn)
在Stream中的操作與直接列表排序類似,可以反轉(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)槭褂昧薙tring排序,也可以寫(xiě)為String::compareTo。如果是復(fù)雜判斷,可以定義一個(gè)更加復(fù)雜的Comparator,組合模式就是這么好用,一層不夠再套一層。
以上就是Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的實(shí)現(xiàn)代碼的詳細(xì)內(nèi)容,更多關(guān)于Java Lambda排序功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用注解@Validated和BindingResult對(duì)入?yún)⑦M(jìn)行非空校驗(yàn)方式
這篇文章主要介紹了使用注解@Validated和BindingResult對(duì)入?yún)⑦M(jìn)行非空校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
詳解Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí)
這篇文章主要為大家詳細(xì)介紹了Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
Java 中使用Spring Security的實(shí)例詳解
Spring Security是一款強(qiáng)大的安全框架,可以幫助用戶保護(hù)Web應(yīng)用程序和REST API的安全性,這篇文章主要介紹了Java 中如何使用Spring Security,需要的朋友可以參考下2023-06-06
spring?boot應(yīng)用無(wú)法啟動(dòng)也沒(méi)報(bào)錯(cuò)信息的解決辦法
在使用Spring Boot開(kāi)發(fā)應(yīng)用程序時(shí),偶爾會(huì)遇到啟動(dòng)不起來(lái)的問(wèn)題,這是一種讓人沮喪的情況,尤其是當(dāng)日志中沒(méi)有任何錯(cuò)誤信息時(shí),這篇文章主要介紹了spring?boot應(yīng)用無(wú)法啟動(dòng)也沒(méi)報(bào)錯(cuò)信息的解決辦法,需要的朋友可以參考下2025-11-11
java實(shí)現(xiàn)遺傳算法實(shí)例分享(打印城市信息)
本文介紹java實(shí)現(xiàn)遺傳算法的實(shí)例,代碼中使用城市名做為數(shù)據(jù),可以打印當(dāng)前代數(shù)的所有城市序列,以及其相關(guān)的參數(shù),大家參考使用吧2014-01-01
Java生成隨機(jī)姓名、性別和年齡的實(shí)現(xiàn)示例
這篇文章主要介紹了Java生成隨機(jī)姓名、性別和年齡的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
java面試常問(wèn)的Runnable和Callable的區(qū)別
大家好,本篇文章主要講的是java面試常問(wèn)的Runnable和Callable的區(qū)別,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
JVM內(nèi)存模型/內(nèi)存空間:運(yùn)行時(shí)數(shù)據(jù)區(qū)
這篇文章主要介紹了JVM內(nèi)存模型/內(nèi)存空間的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java虛擬機(jī),感興趣的朋友可以了解詳細(xì),希望能夠給你帶來(lái)幫助2021-08-08

