java8使用stream的collect进行list转map注意事项
1.创建Person类
package com.xkzhangsan.normal.collectors; public class Person { private Integer id; private String name; private Integer score; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getScore() { return score; } public void setScore(Integer score) { this.score = score; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", score=" + score + "]"; } }
2.创建测试类ListToMap
package com.xkzhangsan.normal.collectors; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.stream.Collectors; public class ListToMap { public static void main(String[] args) { //创建list List<Person> personList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setId(i); p.setName("p"+i); p.setScore(i*10); personList.add(p); } //添加和id=8相同对象score值不同 Person p = new Person(); p.setId(8); p.setName("p"+8); p.setScore(88); personList.add(p); System.out.println("list:========================"); personList.stream().forEach(System.out::println); //转换为HashMap Map<Integer, Person> map = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue)); System.out.println("hashMap:========================"); map.entrySet().stream().forEach(System.out::println); //转换为TreeMap Map<Integer, Person> treeMap = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue, TreeMap::new)); System.out.println("treeMap:========================"); treeMap.entrySet().stream().forEach(System.out::println); } }
3.测试结果
list:======================== Person [id=0, name=p0, score=0] Person [id=1, name=p1, score=10] Person [id=2, name=p2, score=20] Person [id=3, name=p3, score=30] Person [id=4, name=p4, score=40] Person [id=5, name=p5, score=50] Person [id=6, name=p6, score=60] Person [id=7, name=p7, score=70] Person [id=8, name=p8, score=80] Person [id=9, name=p9, score=90] Person [id=8, name=p8, score=88] hashMap:======================== 0=Person [id=0, name=p0, score=0] 1=Person [id=1, name=p1, score=10] 2=Person [id=2, name=p2, score=20] 3=Person [id=3, name=p3, score=30] 4=Person [id=4, name=p4, score=40] 5=Person [id=5, name=p5, score=50] 6=Person [id=6, name=p6, score=60] 7=Person [id=7, name=p7, score=70] 8=Person [id=8, name=p8, score=88] 9=Person [id=9, name=p9, score=90] treeMap:======================== 0=Person [id=0, name=p0, score=0] 1=Person [id=1, name=p1, score=10] 2=Person [id=2, name=p2, score=20] 3=Person [id=3, name=p3, score=30] 4=Person [id=4, name=p4, score=40] 5=Person [id=5, name=p5, score=50] 6=Person [id=6, name=p6, score=60] 7=Person [id=7, name=p7, score=70] 8=Person [id=8, name=p8, score=88] 9=Person [id=9, name=p9, score=90]
4.注意事项
(1)list转map要注意重复对象,map转换方法要选择带mergeFunction参数的方法,如果key值重复,做合并处理,不然会抛异常!可以做到去重效果。
比如上面故意添加和id=8相同对象score为88,值不同。在map转换方法mergeFunction 为(oldValue, newValue)->newValue 使用新对象替换已有老对象,可以看到转换后id8的对象score变为88。
(2)list转map默认转换为HashMap,可以选择带mapSupplier参数的方法,选择要转换为的map类型。
比如上面TreeMap::new,选择转换为TreeMap。
寻找撬动地球的支点(解决问题的方案),杠杆(Java等编程语言)已经有了。xkzhangsan
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具