news 2026/6/12 12:56:07

Java Stream(java.util.stream.*)从 0 到“进阶实战”一次性梳理出来

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java Stream(java.util.stream.*)从 0 到“进阶实战”一次性梳理出来
  1. 创建流(7 种常用入口)

    Stream<String> s1 = Stream.of("a", "b", "c"); Stream<String> s2 = Arrays.stream(arr); Stream<String> s3 = list.stream(); // 集合 Stream<String> s4 = Files.lines(Paths.get("a.txt")); // IO Stream<Integer> s5 = Stream.iterate(0, n -> n + 2); // 无限流 Stream<Integer> s6 = Stream.generate(() -> 1); // 无限流 IntStream s7 = IntStream.range(1, 100); // 基本类型
  2. 中间操作(lazy,链式)

    stream .filter(x -> x % 2 == 0) // 过滤 .map(String::valueOf) // 1:1 转换 .flatMap(x -> Arrays.stream(x.split(","))) // 1:N 打平 .distinct() // 去重 .sorted(Comparator.comparing(User::getAge).reversed()) .peek(System.out::println) // 调试打印,不会触发消费 .limit(5) // 截断 .skip(3) // 跳过 .takeWhile(x -> x < 50) // Java9+ 遇到第一个 false 停止 .dropWhile(x -> x < 50); // Java9+ 丢掉前缀
  3. 终端操作(触发执行)

    // 3.1 遍历/消费 forEach(System.out::println); // 3.2 聚合 long cnt = stream.count(); Optional<Integer> max = stream.max(Integer::compare); Integer sum = stream.mapToInt(Integer::intValue).sum(); OptionalDouble avg = stream.mapToInt(Integer::intValue).average(); // 3.3 规约 Integer total = stream.reduce(0, Integer::sum); String joined = stream.reduce("", (a, b) -> a + "," + b); // 3.4 收集(最灵活) List<String> list = stream.collect(Collectors.toList()); Set<String> set = stream.collect(Collectors.toSet()); Map<String, User> map = stream.collect(Collectors.toMap(User::getId, u -> u)); String csv = stream.collect(Collectors.joining(";")); Map<Integer, List<User>> group = stream.collect(Collectors.groupingBy(User::getAge)); Map<Boolean, List<User>> part = stream.collect(Collectors.partitioningBy(u -> u.getAge() >= 18)); // 3.5 匹配/查找 boolean ok1 = stream.allMatch(x -> x > 0); boolean ok2 = stream.anyMatch(x -> x > 0); boolean ok3 = stream.noneMatch(x -> x > 0); Optional<Integer> first = stream.findFirst(); Optional<Integer> any = stream.findAny(); // 3.6 转数组/基本类型流 int[] arr = stream.mapToInt(Integer::intValue).toArray();
  4. 基本类型特化流(IntStream / LongStream / DoubleStream)

    IntStream.rangeClosed(1, 100) .filter(n -> n % 7 == 0) .average() .ifPresent(System.out::println);
  5. 并行流(parallel)与并发注意

    long c = LongStream.rangeClosed(1, 1_0000_0000L) .parallel() // 拆分成多段 ForkJoin .filter(n -> n % 103 == 0) .count();

    注意:

  • 数据量小别用,线程切换反而慢

  • 避免有状态 lambda、外部共享变量

  • Collectors.toConcurrentMap可进一步减少锁

6.自定义收集器(Collector 接口)

Collector<Integer, StringJoiner, String> myJoin = Collector.of( () -> new StringJoiner("、"), (j, s) -> j.add(s.toString()), (j1, j2) -> j1.merge(j2), StringJoiner::toString); String r = Stream.of(1, 2, 3).collect(myJoin); // 1、2、3

7.与 Optional 联动

stream.map(User::getAddress) .filter(Objects::nonNull) .findFirst() .ifPresent(addr -> System.out.println("找到地址:" + addr));

8.实战 10 例(一行流)

// 1. 去重并按字典序取前 3 List<String> top3 = list.stream().distinct().sorted().limit(3).toList(); // 2. 把两个 List 合并后去重 List<String> union = Stream.concat(list1.stream(), list2.stream()) .distinct() .toList(); // 3. 统计每个单词出现次数 Map<String, Long> freq = words.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // 4. 找到工资最高的员工 Optional<Employee> richest = emps.stream() .max(Comparator.comparingDouble(Employee::getSalary)); // 5. 按部门分组后,取每组工资最高的 Map<Department, Optional<Employee>> topInDept = emps.stream() .collect(Collectors.groupingBy(Employee::getDept, Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)))); // 6. 把 List<User> 转 Map<id, name> Map<Long, String> id2Name = users.stream() .collect(Collectors.toMap(User::getId, User::getName)); // 7. 将字符串列表转整型,跳过非数字 List<Integer> nums = strList.stream() .filter(s -> s.matches("\\d+")) .map(Integer::valueOf) .toList(); // 8. 把多行配置按“键=值”解析成 Map Map<String, String> cfg = lines.stream() .filter(l -> l.contains("=")) .map(l -> l.split("=", 2)) .collect(Collectors.toMap(a -> a[0], a -> a[1])); // 9. 分页:跳过前 (page-1)*size 条,取 size 条 List<Row> page = rows.stream() .skip((pageNo - 1) * pageSize) .limit(pageSize) .toList(); // 10. 递归列出目录下所有 .java 文件 try (Stream<Path> walk = Files.walk(Paths.get("src"))) { List<File> javaFiles = walk.filter(p -> p.toString().endsWith(".java")) .map(Path::toFile) .toList(); }

9.常见坑

  • stream只能消费一次,再调会抛IllegalStateException

  • peek里别改外面状态,调试完就删。

  • parallel不一定快,先度量。

  • toMap遇到重复 key 默认抛异常,用(v1,v2)->v2指定冲突策略。

  • sorted会缓存整个流,大数据慎用。

开发中经常遇到stream的问题,就整理了一份,希望对大家有帮助!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 13:19:48

Windows 11远程桌面多用户配置指南:RDP Wrapper完整教程

还在为Windows 11只能单用户远程连接而烦恼&#xff1f;RDP Wrapper Library这款开源工具能够帮你轻松实现多用户同时远程访问功能&#xff0c;让家庭版系统也能享受企业级的远程桌面体验。无论你是IT管理员、开发者还是普通用户&#xff0c;这份完整配置手册都将为你提供简单实…

作者头像 李华
网站建设 2026/6/12 11:59:36

GitHub热门项目复现:用Qwen-Image-Edit-2509做电商产品图智能修改

GitHub热门项目复现&#xff1a;用Qwen-Image-Edit-2509做电商产品图智能修改 在电商平台的日常运营中&#xff0c;一张主图可能决定一款商品的命运。每逢大促节点&#xff0c;运营团队常常面临这样的困境&#xff1a;几十个SKU需要统一更新价格标签、替换背景文案、调整促销横…

作者头像 李华
网站建设 2026/6/11 21:49:17

9个AI论文工具推荐,本科生期末论文写作轻松搞定

9个AI论文工具推荐&#xff0c;本科生期末论文写作轻松搞定 论文写作的“战场”&#xff1a;时间紧、任务重、压力山大 对于大多数本科生来说&#xff0c;期末论文不仅是对所学知识的一次综合检验&#xff0c;更是对时间管理、写作能力与抗压能力的全面挑战。随着课程内容的不断…

作者头像 李华
网站建设 2026/6/11 18:23:26

大模型微调监控指标:跟踪Qwen3-32B训练过程

大模型微调监控指标&#xff1a;跟踪Qwen3-32B训练过程 在当前大语言模型&#xff08;LLM&#xff09;快速演进的背景下&#xff0c;企业与研究机构正面临一个关键挑战&#xff1a;如何在有限算力资源下&#xff0c;高效微调出性能接近顶级闭源模型的定制化系统。以通义千问系列…

作者头像 李华
网站建设 2026/6/12 3:47:03

8 个文献综述 AI 工具,本科生降重查重率优化推荐

8 个文献综述 AI 工具&#xff0c;本科生降重查重率优化推荐 文献综述的“重担”与时间的“紧逼” 对于大多数本科生来说&#xff0c;论文写作从来不是一件轻松的事情&#xff0c;尤其是当任务涉及到文献综述时&#xff0c;更是让人感到压力山大。文献综述不仅是对已有研究成果…

作者头像 李华