集合常用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 分钟前
Spring Cloud Alibaba(六)-链路追踪SkyWalking
java·后端·spring·skywalking
wuminyu5 分钟前
专家视角看Lambda表达式的原理解析
java·linux·c语言·jvm·c++
wangbing11257 分钟前
Java处理csv文件总是丢数据
java·开发语言·python
云烟成雨TD9 分钟前
Spring AI 1.x 系列【30】向量数据库:核心 API 和入门案例
java·人工智能·spring
许彰午20 分钟前
CacheSQL:一个面向政务系统的内存缓存数据库中间件
java·数据库·缓存·中间件·面试·开源软件·政务
YaBingSec22 分钟前
玄机网络安全靶场:Apache HTTPD 解析漏洞(CVE-2017-15715)WP
java·笔记·安全·web安全·php·apache
书源丶30 分钟前
三十二、Java集合(一)——Collection与List全家桶
java·windows·list
AI人工智能+电脑小能手36 分钟前
【大白话说Java面试题】【Java基础篇】第21题:HashMap和Hashtable的区别是什么
java·开发语言·面试·哈希算法·散列表·hash table
慕容卡卡40 分钟前
Claude 使用神器(web页面)--CloudCLI UI
java·开发语言·前端·人工智能·ui·spring cloud
Sylvia-girl43 分钟前
C++内存如何管理?
java·jvm·c++