当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- How to manage the authority of database account?
- Call analysis of start method in JNI thread and callback analysis of run method
- Epu360: all the H5 templates you want are here, e-book, big turntable, red envelope rain, questionnaire survey
- Open source a set of minimalist front and rear end separation project scaffold
- ES6 learning notes (4): easy to understand the new grammar of ES6
- Hdu3974 assign the task segment tree DFS order
- Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
- C calls SendMessage to refresh the taskbar icon (the icon does not disappear at the end of forcing)
- [byte jumps, autumn recruitment Posts open] ohayoo! Don't leave after school, I want to ask you to play games!!!
- Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
猜你喜欢
STM32F030F4P6兼容灵动微MM32F031F4P6
Metersphere developer's Manual
Zero basis to build a web search engine of its own
行为型模式之解释器模式
Qt音视频开发46-视频传输UDP版
小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
How to start the hidden preferences in coda 2 on the terminal?
Js字符串-String字符串对象方法
轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
Countdown | 2020 PostgreSQL Asia Conference - agenda arrangement of Chinese sub Forum
随机推荐
Hdu3974 assign the task segment tree DFS order
The role of theme music in games
image operating system windows cannot be used on this platform
The isolation level of transaction and its problems
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
Contract trading system development | construction of smart contract trading platform
Unexpected element.. required element
NAND FLASH的接口控制设计
vue3 新特性
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
An article will introduce you to HTML tables and their main attributes
Zero basis to build a web search engine of its own
迅为iMX6开发板-设备树内核-menuconfig的使用
What is the tensor in tensorflow?
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
Jenkins installation and deployment process
How does cglib implement multiple agents?
谷歌浏览器实现视频播放加速功能
STM32F030F4P6兼容灵动微MM32F031F4P6