[Java] Collectors

 

Stream<Employee> emps = ...

TreeSet<Employee> tree = emps.collect(
    Collectors.toCollection(
        () -> new TreeSet<Employee>(
            Comparator.comparingInt(Employee:getSalary)
    )
);

 

Build a map from employees' names t otheir salaries

Stream<Employee> emps = ...

Map<String, Integer> salaries = emps.collect(
    Collectors.toMap(Employee::getName, Employee::getSalary)
);

 

Group employees by Salary brackets

first bracket: 0-999

2nd bracket: 1000-1999

Stream<Employee> emps = ...

Map<Integer, List<Employee>> brackets = 
    emps.collect(Collectors.groupBy(
        e -> e.getSalary()/1000
    )
);

 

posted @   Zhentiw  阅读(137)  评论(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工具
历史上的今天:
2019-01-22 [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction
2019-01-22 [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
2018-01-22 [React] Optimistic UI update in React using setState()
2018-01-22 [Javascript] Required function arguments in Javascript
2017-01-22 [Vue] Update Attributes, Classes and Styles in Vue.js with v-bind
点击右上角即可分享
微信分享提示