JDK 1.8 Java Stream 最全操作整理
·
基于 JDK 1.8 的 Java Stream 常用操作大全,涵盖开发中最常用的所有场景(过滤、排序、分组、求和、聚合、去重、映射、合并、多条件排序等)。
所有代码都是 JDK1.8 兼容的。
⭐ JDK 1.8 Java Stream 最全操作整理
1. 创建 Stream
Stream<String> stream1 = list.stream();
Stream<String> stream2 = Stream.of("a", "b", "c");
2. 遍历 forEach
list.stream().forEach(System.out::println);
3. 过滤 filter
List<String> filtered = list.stream()
.filter(s -> s.length() > 3)
.collect(Collectors.toList());
4. 排序 sorted
4.1 默认排序
List<String> sorted = list.stream()
.sorted()
.collect(Collectors.toList());
4.2 按对象字段排序
list.stream()
.sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
4.3 多字段排序(先 age,后 name)
list.stream()
.sorted(
Comparator.comparing(User::getAge)
.thenComparing(User::getName)
)
.collect(Collectors.toList());
4.4 倒序排序
list.stream()
.sorted(Comparator.comparing(User::getAge).reversed())
.collect(Collectors.toList());
5. 转换 map
5.1 字段提取
List<String> names = list.stream()
.map(User::getName)
.collect(Collectors.toList());
5.2 对元素做运算
List<Integer> lengths = list.stream()
.map(String::length)
.collect(Collectors.toList());
6. 去重 distinct
list.stream().distinct().collect(Collectors.toList());
7. 去重(按对象某个字段)
list.stream()
.filter(StreamDistinctByKey.distinctByKey(User::getId))
.collect(Collectors.toList());
工具方法:
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
8. 转 Map(key-value)
8.1 简单对象转 Map
Map<Long, String> map = list.stream()
.collect(Collectors.toMap(User::getId, User::getName));
8.2 key 冲突处理(后者覆盖前者)
.collect(Collectors.toMap(User::getId, User::getName, (a, b) -> b));
9. 分组 groupingBy
9.1 按字段分组
Map<String, List<User>> map = list.stream()
.collect(Collectors.groupingBy(User::getCity));
9.2 分组并统计数量
Map<String, Long> countMap = list.stream()
.collect(Collectors.groupingBy(User::getCity, Collectors.counting()));
9.3 分组求和
Map<String, Integer> sumMap = list.stream()
.collect(Collectors.groupingBy(User::getCity,
Collectors.summingInt(User::getAge)));
10. 聚合操作(sum / max / min)
10.1 求和
int sum = list.stream()
.mapToInt(User::getAge)
.sum();
10.2 最大值
User max = list.stream()
.max(Comparator.comparing(User::getAge))
.orElse(null);
10.3 最小值
User min = list.stream()
.min(Comparator.comparing(User::getAge))
.orElse(null);
11. 匹配操作 anyMatch / allMatch / noneMatch
boolean any = list.stream().anyMatch(u -> u.getAge() > 18);
boolean all = list.stream().allMatch(u -> u.getAge() > 18);
boolean none = list.stream().noneMatch(u -> u.getAge() < 0);
12. 截取 limit / skip
12.1 取前 10 条
list.stream().limit(10).collect(Collectors.toList());
12.2 跳过前 10 条
list.stream().skip(10).collect(Collectors.toList());
13. flatMap(多层结构展开)
例如:List<List>
List<String> flat = list.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
14. 合并两个 Stream
Stream.concat(stream1, stream2)
.collect(Collectors.toList());
15. Optional 结合 Stream
Optional<User> first = list.stream()
.filter(u -> u.getAge() > 20)
.findFirst();
16. 并行 Stream (parallel)
注意:多线程环境要小心!!!
list.parallelStream()
.map(...)
.collect(Collectors.toList());
✔ 如你问过的例子:
对 ['000','001','010'] 排序
List<String> list = Arrays.asList("000","001","010");
List<String> sorted = list.stream()
.sorted()
.collect(Collectors.toList());
更多推荐

所有评论(0)