集合常用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())
    ));
相关推荐
金融数据出海3 分钟前
黄金、碳排放期货市场API接口文档
java·开发语言·spring boot·后端·金融·区块链
胡斌附体8 分钟前
微服务中 本地启动 springboot 无法找到nacos配置 启动报错
java·spring boot·微服务·yml·naocs yml
薯条不要番茄酱28 分钟前
【JVM】从零开始深度解析JVM
java·jvm
夏季疯31 分钟前
学习笔记:黑马程序员JavaWeb开发教程(2025.3.31)
java·笔记·学习
D_aniel_37 分钟前
排序算法-快速排序
java·排序算法·快速排序
长安城没有风39 分钟前
数据结构 集合类与复杂度
java·数据结构
D_aniel_42 分钟前
排序算法-插入排序
java·排序算法·插入排序
brevity_souls44 分钟前
java面试OOM汇总
java·开发语言·面试
假女吖☌1 小时前
Maven 处理依赖冲突
java·python·maven
eguid_11 小时前
WebRTC工作原理详细介绍、WebRTC信令交互过程和WebRTC流媒体传输协议介绍
java·音视频·webrtc·实时音视频