当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- The native API of the future trend of the front end: web components
- 2020-08-15:什么情况下数据任务需要优化?
- The isolation level of transaction and its problems
- Some operations kept in mind by the front end foundation GitHub warehouse management
- 2020-09-04:函数调用约定了解么?
- An article taught you to use HTML5 SVG tags
- Method of code refactoring -- Analysis of method refactoring
- 细数软件工程----各阶段必不可少的那些图
- How much disk space does a new empty file take?
- ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
猜你喜欢
Understanding formatting principles
[learning] interface test case writing and testing concerns
An article will introduce you to HTML tables and their main attributes
Nodejs中使用jsonwebtoken(JWT)生成token的场景使用
Common syntax corresponding table of mongodb and SQL
Python basic data type -- tuple analysis
How to make characters move
Qt音视频开发46-视频传输UDP版
The legality of IPFs / filecoin: protecting personal privacy from disclosure
消防器材RFID固定资产管理系统
随机推荐
Jenkins installation and deployment process
An article will take you to understand CSS3 fillet knowledge
C and C / C + + mixed programming series 5 - GC collaboration of memory management
Stickinengine architecture 12 communication protocol
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
Application insights application insights use application maps to build request link views
Using an example to understand the underlying processing mechanism of JS function
轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
行为型模式之解释器模式
Method of code refactoring -- Analysis of method refactoring
ES6 learning notes (2): teach you to play with class inheritance and class objects
Zero basis to build a web search engine of its own
To solve the problem that the data interface is not updated after WPF binding set
File download manager realized by electron
Big data processing black Technology: revealing the parallel computing technology of Pb level data warehouse gaussdb (DWS)
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
Visual rolling [contrast beauty]
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
Road to simple HTML + JS to achieve the most simple game Tetris