集合常用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())
    ));
相关推荐
Code blocks44 分钟前
关于“LoggerFactory is not a Logback LoggerContext but Logback is on ......“的解决方案
java·spring boot·后端
04Koi.3 小时前
八股训练--Spring
java·后端·spring
Dcs4 小时前
微软 Copilot 被“越狱”?安全研究员教你一招拿下“沙箱环境 Root 权限”!
java
℡余晖^4 小时前
每日面试题18:基本数据类型和引用数据类型的区别
java
hello 早上好5 小时前
消息顺序、消息重复问题
java·中间件
phltxy5 小时前
ArrayList与顺序表
java·算法
Doris_LMS5 小时前
保姆级别IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
java·mysql·postgresql
衍生星球5 小时前
JSP 程序设计之 Web 技术基础
java·开发语言·jsp
Java编程乐园6 小时前
Java函数式编程之【Stream终止操作】【下】【三】【收集操作collect()与分组分区】【下游收集器】
java
yinyan13146 小时前
一起学springAI系列一:初体验
java·人工智能·ai