集合常用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())
    ));
相关推荐
金銀銅鐵9 小时前
[Java] 如何自动生成简单的 Mermaid 类图
java·后端
纵横八荒10 小时前
Java基础加强13-集合框架、Stream流
java·开发语言
稚辉君.MCA_P8_Java10 小时前
kafka解决了什么问题?mmap 和sendfile
java·spring boot·分布式·kafka·kubernetes
乄bluefox10 小时前
保姆级docker部署nacos集群
java·docker·容器
欣然~10 小时前
百度地图收藏地址提取与格式转换工具 说明文档
java·开发语言·dubbo
玩毛线的包子10 小时前
Android Gradle学习(十三)- 配置读取和文件写入
java
青岛少儿编程-王老师11 小时前
CCF编程能力等级认证GESP—C++6级—20250927
java·c++·算法
一條狗11 小时前
学习日报 20251007|深度解析:基于 Guava LoadingCache 的优惠券模板缓存设计与实现
java·oracle·loadingcache
Miraitowa_cheems12 小时前
LeetCode算法日记 - Day 64: 岛屿的最大面积、被围绕的区域
java·算法·leetcode·决策树·职场和发展·深度优先·推荐算法
Lisonseekpan12 小时前
Spring Boot 中使用 Caffeine 缓存详解与案例
java·spring boot·后端·spring·缓存