[Java Stream] Basic terminal operations

To void: 

  forEach, forEachOrdered, peek

stream.peek(System.out::println) // print without termination
    .filter(n -> n > 0)
    .distinct()
    .limit(10)
    .forEach(System.out::println);

 

To boolean:

  allMatch, anyMatch, noneMatch

Collection<Employee> emps = ...;

boolean allValid = emps.stream()
    .allMatch(e -> e.getName != null && e.getName().length() > 0);

 

To array:

  toArray

Stream<Employee> emps = ...;

Object[] lowEmps = emps.filter(e -> e.getSalary() < 2000)
                                      .toArray();
Employee[] lowEMps = emps.filter(e -> e.getSalary() < 2000)
                                          .toArray(Employee[]::new);

 

To long:

  count

To T:

  findFirst, findAny, min, max

String result = stream.min(comparator).orElse("default string");
Collection<String> strings = ...;

Optional<String> longest = 
    strings.stream()
               .max(Comparator.comparingInt(String::length))

 

posted @   Zhentiw  阅读(78)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-01-21 [XState] Invoke Child XState Machines from a Parent Machine
2020-01-21 [XState] Invoke Callbacks to Send and Receive Events from a Parent XState Machine
2020-01-21 [XState] Invoking a Promise for Asynchronous State Transitions in XState
2020-01-21 [Algorithm] 977. Squares of a Sorted Array
2020-01-21 [XState] Delay XState Events and Transitions
2020-01-21 [XState] Use XState Null Events and Transient Transitions to Immediately Transition States
2019-01-21 [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
点击右上角即可分享
微信分享提示