集合常用Stream操作

1、中间操作

filter()过滤

将流中的元素筛选出满足条件的元素

复制代码
List<String> list = Arrays.asList("abc","test","demo","frse","fesfes");
list.stream().filter(s -> s.startsWith("f")).forEach(System.out::println);

map()映射转换

将流中的每个元素通过特定的函数转换为另一个流。

语法:<R> Stream<R> map(Function<? super T, ? extends R> mapper)

复制代码
list.stream().map(String::toUpperCase).forEach(System.out::println);
//获取对象元素
userList.stream().map(User::getName).forEach(System.out::println);

distinct()去重

复制代码
list.stream().distinct().forEach(System.out::println);

sorted()排序

复制代码
list.stream().sorted().forEach(System.out::println);
list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);

2、终端操作

foreach()遍历

复制代码
list.stream().forEach(System.out::println);

collect()收集结果

复制代码
List<String> upperList = list.stream()
                            .map(String::toUpperCase)
                            .collect(Collectors.toList());

Set<String> set = list.stream().collect(Collectors.toSet());

String joined = list.stream().collect(Collectors.joining(", "));

toArray()转为数组

复制代码
String[] array = list.stream().toArray(String[]::new);

reduce()归约

复制代码
Optional<String> reduced = list.stream().reduce((s1, s2) -> s1 + "#" + s2);

int sum = numbers.stream().reduce(0, Integer::sum);

count()统计

复制代码
long count = list.stream().count();

anyMatch()/allMatch()/noneMatch() 匹配检查

复制代码
boolean anyStartsWithA = list.stream().anyMatch(s -> s.startsWith("a"));
boolean allStartsWithA = list.stream().allMatch(s -> s.startsWith("a"));
boolean noneStartsWithA = list.stream().noneMatch(s -> s.startsWith("a"));

findFirst()/findAny()查找元素

复制代码
Optional<String> first = list.stream().findFirst();
Optional<String> any = list.stream().findAny();

3、Collectors工具类

toList()/toSet()/toCollection()

复制代码
List<String> list = stream.collect(Collectors.toList());
Set<String> set = stream.collect(Collectors.toSet());
TreeSet<String> treeSet = stream.collect(Collectors.toCollection(TreeSet::new));
//转为map
Map<String, Integer> userMap = userList.stream().collect(Collectors.toMap(User::getName, User::getAge));

joining()字符串连接

复制代码
String joined = stream.collect(Collectors.joining(", ", "[", "]"));

summarizingInt()/summarizingDouble()统计

复制代码
IntSummaryStatistics stats = stream.collect(Collectors.summarizingInt(String::length));

groupingBy()分组

复制代码
Map<Integer, List<String>> groupByLength = 
    stream.collect(Collectors.groupingBy(String::length));

Map<Integer, Long> countByLength = 
    stream.collect(Collectors.groupingBy(String::length, Collectors.counting()));

mapping()下游收集器

复制代码
Map<Integer, List<Character>> firstCharByLength = 
    stream.collect(Collectors.groupingBy(
        String::length,
        Collectors.mapping(s -> s.charAt(0), Collectors.toList())
    ));
相关推荐
没有bug.的程序员7 分钟前
云原生与分布式架构的完美融合:从理论到生产实践
java·分布式·微服务·云原生·架构
村口张大爷17 分钟前
Spring Boot 初始化钩子
java·spring boot·后端
x_feng_x21 分钟前
Java从入门到精通 - 集合框架(二)
java·开发语言·windows
LB211222 分钟前
苍穹外卖-缓存套餐 Spring Cache day07
java·spring boot·spring
Le1Yu32 分钟前
雪崩问题及其解决方案(请求限流、线程隔离、服务熔断、fallback、sentinel实现以上功能)
java·开发语言
徐子童39 分钟前
基于微服务的在线判题系统重点总结
java·微服务·架构
青衫码上行1 小时前
【从0开始学习Java | 第21篇】网络编程综合练习
java·网络·学习
鸽鸽程序猿1 小时前
【项目】【抽奖系统】注册功能实现
java·开发语言
沐浴露z2 小时前
【JVM】详解 运行时数据区
java·jvm
云泽8082 小时前
C/C++内存管理详解:从基础原理到自定义内存池原理
java·c语言·c++