Java中Stream實現(xiàn)List排序的六個核心技巧總結(jié)
更新時間:2025年04月22日 09:33:15 作者:藥師YS
這篇文章主要介紹了Java中Stream實現(xiàn)List排序的六個核心技巧,分別是自然序排序、反向排序、空值安全處理、多字段組合排序、并行流加速、原地排序等,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
一、基礎(chǔ)排序?qū)崿F(xiàn)
1.1 自然序排序(正序)
List<Entity> sortedList = originalList.stream() .sorted(Comparator.comparing(Entity::getId)) .collect(Collectors.toList());
1.2 反向排序(倒序)
List<Entity> sortedList = originalList.stream() .sorted(Comparator.comparing(Entity::getId).reversed()) .collect(Collectors.toList());
二、進階排序技巧
2.1 空值安全處理
// 處理可能為null的字段 Comparator<Entity> nullSafeComparator = Comparator.comparing( Entity::getId, Comparator.nullsFirst(Comparator.naturalOrder()) ); List<Entity> sortedList = originalList.stream() .sorted(nullSafeComparator) .collect(Collectors.toList());
2.2 多字段組合排序
List<Entity> sortedList = originalList.stream() .sorted(Comparator.comparing(Entity::getDepartment) .thenComparing(Entity::getId)) .collect(Collectors.toList());
三、性能優(yōu)化建議
3.1 并行流加速(適用于大數(shù)據(jù)量)
List<Entity> sortedList = originalList.parallelStream() .sorted(Comparator.comparing(Entity::getId)) .collect(Collectors.toList());
3.2 原地排序(修改原集合)
originalList.sort(Comparator.comparing(Entity::getId));
四、最佳實踐
- 類型明確化:推薦指定具體集合類型
ArrayList<Entity> sortedList = originalList.stream() .sorted(Comparator.comparing(Entity::getId)) .collect(Collectors.toCollection(ArrayList::new));
- 防御性拷貝:保持原集合不可變
List<Entity> sortedList = new ArrayList<>(originalList); sortedList.sort(Comparator.comparing(Entity::getId));
- Lambda優(yōu)化:復(fù)雜場景使用Lambda表達式
List<Entity> sortedList = originalList.stream() .sorted((e1, e2) -> { // 自定義比較邏輯 return e1.getId().compareTo(e2.getId()); }) .collect(Collectors.toList());
五、注意事項
- 不可變性:
Collectors.toList()
返回的List實現(xiàn)可能不支持修改 - 空指針防護:推薦始終使用
Comparator.nullsFirst/nullsLast
- 性能權(quán)衡:超過10萬條數(shù)據(jù)時優(yōu)先考慮傳統(tǒng)排序方式
- 對象狀態(tài):Stream操作不會修改原始集合元素
六、完整示例
public class SortingDemo { public static void main(String[] args) { List<Entity> entities = Arrays.asList( new Entity(2, "B"), new Entity(1, "A"), new Entity(3, "C") ); // 多條件排序:先按名稱倒序,再按ID正序 List<Entity> sorted = entities.stream() .sorted(Comparator.comparing(Entity::getName) .reversed() .thenComparing(Entity::getId)) .collect(Collectors.toList()); sorted.forEach(System.out::println); } } class Entity { private int id; private String name; // 構(gòu)造方法和getter省略 }
七、總結(jié)對比
排序方式 | 時間復(fù)雜度 | 空間復(fù)雜度 | 適用場景 |
---|---|---|---|
Stream順序流 | O(n log n) | O(n) | 通用場景 |
Stream并行流 | O(n log n) | O(n) | 大數(shù)據(jù)量(10w+) |
Collections.sort | O(n log n) | O(1) | 原地修改需求 |
數(shù)據(jù)庫排序 | O(n log n) | O(1) | 數(shù)據(jù)源在數(shù)據(jù)庫時 |
通過合理選擇排序策略,可以在保證代碼簡潔性的同時兼顧系統(tǒng)性能。建議根據(jù)實際業(yè)務(wù)場景選擇最合適的排序方式。
到此這篇關(guān)于Java中Stream實現(xiàn)List排序的六個核心技巧的文章就介紹到這了,更多相關(guān)Java Stream實現(xiàn)List排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-boot使用Admin監(jiān)控應(yīng)用的方法
本篇文章主要介紹了spring-boot使用Admin監(jiān)控應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Springboot內(nèi)嵌tomcat應(yīng)用原理深入分析
懂得SpringBoot的童鞋應(yīng)該很清楚,不管應(yīng)用程序是屬于何種類型,都是一個Main方法走遍天下,對于web應(yīng)用,只需要引入spring-boot-starter-web中這個依賴,應(yīng)用程序就好像直接給我們來了個tomcat一樣,對于嵌入式Tomcat,其實也非常簡單,就是調(diào)用Tomcat提供的外部類2022-09-09java int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法
下面小編就為大家?guī)硪黄猨ava int轉(zhuǎn)byte和long轉(zhuǎn)byte的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10