Java?Stream?的?sorted實(shí)現(xiàn)自定義排序從基礎(chǔ)到高級(jí)技巧
Java Stream API 中的 sorted() 方法是一個(gè)強(qiáng)大的中間操作,它允許我們對(duì)流中的元素進(jìn)行排序。默認(rèn)情況下,sorted() 要求元素實(shí)現(xiàn) Comparable 接口,但在實(shí)際應(yīng)用中,我們經(jīng)常需要根據(jù)特定業(yè)務(wù)規(guī)則進(jìn)行自定義排序。本文將深入探討如何使用 sorted() 方法實(shí)現(xiàn)自定義排序,涵蓋各種常見場(chǎng)景和高級(jí)技巧。
一、sorted 方法基礎(chǔ)
Java Stream 提供了兩種 sorted() 方法重載:
自然排序:要求元素實(shí)現(xiàn) Comparable 接口
Stream<T> sorted()
自定義排序:通過 Comparator 指定排序規(guī)則
Stream<T> sorted(Comparator<? super T> comparator)
二、自定義排序的基本實(shí)現(xiàn)
1. 使用 Lambda 表達(dá)式創(chuàng)建 Comparator
// 示例1:按字符串長(zhǎng)度排序
List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
List<String> sortedByLength = words.stream()
.sorted((s1, s2) -> s1.length() - s2.length())
.toList();
System.out.println(sortedByLength); // 輸出:[date, apple, cherry, banana]
// 示例2:按絕對(duì)值大小排序
List<Integer> numbers = Arrays.asList(-5, 2, -8, 1, 3);
List<Integer> sortedByAbs = numbers.stream()
.sorted((n1, n2) -> Math.abs(n1) - Math.abs(n2))
.toList();
System.out.println(sortedByAbs); // 輸出:[1, 2, 3, -5, -8]2. 使用 Comparator 靜態(tài)方法簡(jiǎn)化代碼
Java 8 為 Comparator 接口提供了許多實(shí)用的靜態(tài)方法,使排序代碼更加簡(jiǎn)潔:
// 使用 Comparator.comparing 方法
List<String> sortedByLength2 = words.stream()
.sorted(Comparator.comparing(String::length))
.toList();
// 使用 Comparator.comparingInt 優(yōu)化基本類型比較
List<Integer> sortedByAbs2 = numbers.stream()
.sorted(Comparator.comparingInt(Math::abs))
.toList();三、處理復(fù)雜對(duì)象排序
1. 對(duì)自定義對(duì)象按屬性排序
class Person {
private String name;
private int age;
private LocalDate birthDate;
// 構(gòu)造方法、getter和setter略
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + ", birthDate=" + birthDate + "}";
}
}
// 按年齡升序排序
List<Person> people = Arrays.asList(
new Person("Alice", 25, LocalDate.of(2000, 1, 1)),
new Person("Bob", 20, LocalDate.of(2005, 5, 5)),
new Person("Charlie", 30, LocalDate.of(1995, 10, 10))
);
List<Person> sortedByAge = people.stream()
.sorted(Comparator.comparingInt(Person::getAge))
.toList();
System.out.println(sortedByAge);
// 輸出:[Person{name='Bob', age=20}, Person{name='Alice', age=25}, Person{name='Charlie', age=30}]2. 多條件排序(復(fù)合排序)
使用 thenComparing() 方法可以實(shí)現(xiàn)多級(jí)排序:
// 先按年齡升序,年齡相同則按出生日期降序
List<Person> sortedByAgeAndBirthDate = people.stream()
.sorted(Comparator.comparingInt(Person::getAge)
.thenComparing(Person::getBirthDate, Comparator.reverseOrder()))
.toList();
System.out.println(sortedByAgeAndBirthDate);
四、處理空值與 null 安全排序
1. 空值處理策略
在實(shí)際應(yīng)用中,集合元素或元素屬性可能為 null,直接排序會(huì)導(dǎo)致 NullPointerException。我們可以使用 Comparator.nullsFirst() 或 Comparator.nullsLast() 來安全處理 null 值:
// 示例:處理可能為 null 的字符串
List<String> stringsWithNulls = Arrays.asList("apple", null, "banana", null, "cherry");
// null 值排在前面
List<String> sortedWithNullsFirst = stringsWithNulls.stream()
.sorted(Comparator.nullsFirst(Comparator.naturalOrder()))
.toList();
System.out.println(sortedWithNullsFirst);
// 輸出:[null, null, apple, banana, cherry]
// null 值排在后面
List<String> sortedWithNullsLast = stringsWithNulls.stream()
.sorted(Comparator.nullsLast(Comparator.naturalOrder()))
.toList();
System.out.println(sortedWithNullsLast);
// 輸出:[apple, banana, cherry, null, null]2. 對(duì)象屬性可能為 null 的情況
class Product {
private String name;
private Double price; // 價(jià)格可能為 null
// 構(gòu)造方法、getter和setter略
}
List<Product> products = Arrays.asList(
new Product("Laptop", 1200.0),
new Product("Mouse", null),
new Product("Keyboard", 50.0)
);
// 按價(jià)格排序,null 價(jià)格排在最后
List<Product> sortedByPrice = products.stream()
.sorted(Comparator.comparing(Product::getPrice, Comparator.nullsLast(Double::compare)))
.toList();五、逆序排序與自定義比較邏輯
1. 逆序排序
使用 Comparator.reverseOrder() 或 Comparator.comparing().reversed() 實(shí)現(xiàn)逆序:
// 字符串長(zhǎng)度逆序排序
List<String> reversedByLength = words.stream()
.sorted(Comparator.comparing(String::length).reversed())
.toList();
// 另一種逆序?qū)懛?
List<String> reversedByLength2 = words.stream()
.sorted((s1, s2) -> s2.length() - s1.length())
.toList();2. 自定義復(fù)雜比較邏輯
對(duì)于更復(fù)雜的業(yè)務(wù)規(guī)則,可以實(shí)現(xiàn) Comparator 接口:
// 示例:按字符串長(zhǎng)度排序,長(zhǎng)度相同則按字母順序排序
List<String> complexSort = words.stream()
.sorted(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int lengthCompare = Integer.compare(s1.length(), s2.length());
if (lengthCompare != 0) {
return lengthCompare;
}
return s1.compareTo(s2);
}
})
.toList();
// 使用 Lambda 簡(jiǎn)化
List<String> complexSort2 = words.stream()
.sorted((s1, s2) -> {
int lengthCompare = Integer.compare(s1.length(), s2.length());
return lengthCompare != 0 ? lengthCompare : s1.compareTo(s2);
})
.toList();六、性能優(yōu)化與注意事項(xiàng)
1. 基本類型與裝箱類型
對(duì)于基本類型(如 int、long、double),優(yōu)先使用 comparingInt、comparingLong、comparingDouble 避免裝箱拆箱開銷:
// 性能優(yōu)化示例
List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 2);
// 避免裝箱
List<Integer> optimizedSort = numbers.stream()
.sorted(Comparator.comparingInt(Integer::intValue))
.toList();2. 排序穩(wěn)定性
Stream.sorted() 使用的是穩(wěn)定排序算法(TimSort),即相等元素的相對(duì)順序不會(huì)改變。這在多級(jí)排序中尤為重要:
// 示例:先按部門排序,再按工資排序
List<Employee> employees = ...;
List<Employee> sortedEmployees = employees.stream()
.sorted(Comparator.comparing(Employee::getDepartment)
.thenComparingDouble(Employee::getSalary))
.toList();3. 并行流排序性能
在并行流中,排序操作可能會(huì)導(dǎo)致性能下降,因?yàn)樾枰謹(jǐn)?shù)據(jù)重組。謹(jǐn)慎在并行流中使用復(fù)雜排序:
// 并行流排序示例
List<Integer> parallelSorted = numbers.parallelStream()
.sorted()
.toList();
七、實(shí)戰(zhàn)案例
1. 電商商品排序系統(tǒng)
class Product {
private String name;
private double price;
private int salesVolume;
private LocalDateTime createTime;
// 構(gòu)造方法、getter和setter略
}
// 按價(jià)格升序排序
List<Product> sortedByPrice = products.stream()
.sorted(Comparator.comparingDouble(Product::getPrice))
.toList();
// 按銷量降序,銷量相同則按創(chuàng)建時(shí)間降序
List<Product> sortedBySalesAndTime = products.stream()
.sorted(Comparator.comparingInt(Product::getSalesVolume).reversed()
.thenComparing(Product::getCreateTime, Comparator.reverseOrder()))
.toList();2. 日志時(shí)間戳排序
class LogEntry {
private LocalDateTime timestamp;
private String message;
private LogLevel level;
// 構(gòu)造方法、getter和setter略
}
// 按時(shí)間戳排序
List<LogEntry> sortedLogs = logs.stream()
.sorted(Comparator.comparing(LogEntry::getTimestamp))
.toList();
// 按日志級(jí)別排序(自定義順序:ERROR > WARN > INFO > DEBUG)
List<LogEntry> sortedByLevel = logs.stream()
.sorted(Comparator.comparing(LogEntry::getLevel,
Comparator.comparingInt(level -> {
switch (level) {
case ERROR: return 4;
case WARN: return 3;
case INFO: return 2;
case DEBUG: return 1;
default: return 0;
}
}).reversed()))
.toList();八、總結(jié)與最佳實(shí)踐
優(yōu)先使用方法引用和靜態(tài)工具方法:
// 推薦寫法 sorted(Comparator.comparing(Person::getAge)) // 避免冗余的 Lambda sorted((p1, p2) -> p1.getAge() - p2.getAge())
多級(jí)排序使用鏈?zhǔn)秸{(diào)用:
sorted(Comparator.comparing(Person::getDepartment)
.thenComparing(Person::getAge)
.thenComparing(Person::getName))
處理 null 值:
sorted(Comparator.nullsLast(Comparator.comparing(Person::getName)))
基本類型優(yōu)化:
sorted(Comparator.comparingInt(Person::getAge)) // 避免裝箱
復(fù)雜比較器提取為常量:
public static final Comparator<Person> AGE_NAME_COMPARATOR =
Comparator.comparingInt(Person::getAge)
.thenComparing(Person::getName);
// 使用時(shí)
sorted(AGE_NAME_COMPARATOR)通過掌握 sorted() 方法的各種用法,你可以靈活應(yīng)對(duì)各種復(fù)雜的排序需求,編寫出簡(jiǎn)潔、高效且易于維護(hù)的代碼。在實(shí)際開發(fā)中,合理運(yùn)用 Comparator 的各種工具方法和特性,能夠顯著提升代碼質(zhì)量和開發(fā)效率。
到此這篇關(guān)于java多線程的文章就介紹到這了,更多相關(guān)java多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 一文詳解Java Stream的sorted自定義排序
- 關(guān)于stream().sorted()以及java中常用的比較器排序
- Java8排序stream.sorted()的使用
- Java8 使用 stream().sorted()對(duì)List集合進(jìn)行排序的操作
- Java中LambdaQueryWrapper設(shè)置自定義排序代碼示例
- Java如何實(shí)現(xiàn)List自定義排序
- 詳解JAVA使用Comparator接口實(shí)現(xiàn)自定義排序
- Java針對(duì)ArrayList自定義排序的2種實(shí)現(xiàn)方法
- java Lucene 中自定義排序的實(shí)現(xiàn)
相關(guān)文章
java實(shí)現(xiàn)二分法查找出數(shù)組重復(fù)數(shù)字
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)二分法查找出數(shù)組重復(fù)數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
springmvc請(qǐng)求轉(zhuǎn)發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù))
這篇文章主要介紹了springmvc請(qǐng)求轉(zhuǎn)發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
詳解Spring AOP的原理與實(shí)現(xiàn)方式
Spring框架是一個(gè)功能強(qiáng)大且靈活的企業(yè)級(jí)應(yīng)用程序開發(fā)框架,其中最重要的特性之一就是面向切面編程(AOP),我們今天這篇文章將從源碼和案例的角度詳細(xì)介紹Spring AOP的思想、原理和實(shí)現(xiàn)方式2023-07-07

