当前位置:网站首页>List to map (split the list according to the key, and the value of the same key is a list)
List to map (split the list according to the key, and the value of the same key is a list)
2020-11-06 21:45:00 【Architects who can drive】
/**
* @description according to key Split list
* @data ${DATA}
* @auther
*/
public class MyListTOMap {
public static void main(String[] args) {
UserTest user1 = new UserTest(10,"xiaoming");
UserTest user2 = new UserTest(20,"zhongming");
UserTest user3 = new UserTest(30,"daming");
UserTest user4 = new UserTest(30,"i'mgoal");
List<UserTest> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
Map<Integer,List<UserTest>> mapTest = new HashMap<>();
for (UserTest u: list) {
if(mapTest.containsKey(u.getAge())){
mapTest.get(u.getAge()).add(u);
}else{
List<UserTest> tempList = new ArrayList<>();
tempList.add(u);
mapTest.put(u.getAge(),tempList);
}
}
list.forEach(System.out::println);
System.out.println(mapTest);
}
}
版权声明
本文为[Architects who can drive]所创,转载请带上原文链接,感谢
边栏推荐
- With this artifact, quickly say goodbye to spam messages
- win7 APPCRASH(解决方法)(转)
- Can you do it with only six characters?
- 2020-09-09:裸写算法:两个线程轮流打印数字1-100。
- Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
- 上海巨微专用蓝牙广播芯片
- C and C / C + + mixed programming series 5 - GC collaboration of memory management
- How to make characters move
- Method of code refactoring -- Analysis of method refactoring
- ado.net and asp.net The relationship between
猜你喜欢
Js数组-数组的用法全在这里(数组方法的重构、数组的遍历、数组的去重,数组的判断与转换)
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
非易失性MRAM存储器应用于各级高速缓存
An article takes you to understand CSS gradient knowledge
2020 database technology conference helps technology upgrade
An article taught you to use HTML5 SVG tags
2020-08-29:进程线程的区别,除了包含关系之外的一些区别,底层详细信息?
Junit测试出现 empty test suite
Exclusive interview with Alibaba cloud database for 2020 PostgreSQL Asia Conference: Zeng Wenjing
Qt音视频开发46-视频传输UDP版
随机推荐
打工人好物——磨炼钢铁意志就要这样高效的电脑
ES6 learning notes (2): teach you to play with class inheritance and class objects
What the hell is fastthreadlocal? The existence of ThreadLocal!!
小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
消防器材RFID固定资产管理系统
事务的本质和死锁的原理
Unexpected element.. required element
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
Understanding formatting principles
统计项目代码行数
DC-1 target
An article will take you to understand CSS3 fillet knowledge
jenkins安装部署过程简记
Elasticsearch database | elasticsearch-7.5.0 application construction
Introduction to Huawei cloud micro certification examination
Vue communication and cross component listening state Vue communication
An article will take you to understand CSS alignment
实用工具类函数(持续更新)
Detect certificate expiration script
Using an example to understand the underlying processing mechanism of JS function