当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- ES6 learning notes (5): easy to understand ES6's built-in extension objects
- Some operations kept in mind by the front end foundation GitHub warehouse management
- Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
- 2020-08-20:GO语言中的协程与Python中的协程的区别?
- Visual rolling [contrast beauty]
- Common syntax corresponding table of mongodb and SQL
- All the way, I was forced to talk about C code debugging skills and remote debugging
- Python 100 cases
- MRAM高速缓存的组成
- Small program introduction to proficient (2): understand the four important files of small program development
猜你喜欢
CloudQuery V1.2.0 版本发布
An article will introduce you to HTML tables and their main attributes
Using an example to understand the underlying processing mechanism of JS function
Stickinengine architecture 11 message queue
Some operations kept in mind by the front end foundation GitHub warehouse management
An article takes you to understand CSS3 picture border
Those who have worked in China for six years and a million annual salary want to share these four points with you
轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
Unity performance optimization
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
随机推荐
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
2020-09-09:裸写算法:两个线程轮流打印数字1-100。
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
Vue communication and cross component listening state Vue communication
Detect certificate expiration script
How much disk space does a file of 1 byte actually occupy
预留电池接口,内置充放电电路及电量计,迅为助力轻松搞定手持应用
Js字符串-String字符串对象方法
An article will introduce you to HTML tables and their main attributes
非易失性MRAM存储器应用于各级高速缓存
Those who have worked in China for six years and a million annual salary want to share these four points with you
The 4th China BIM (digital construction) manager Summit Forum will be held in Hangzhou in 2020
How to make characters move
The native API of the future trend of the front end: web components
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
#JVM 类加载机制
C and C / C + + mixed programming series 5 - GC collaboration of memory management
Countdown | 2020 PostgreSQL Asia Conference - agenda arrangement of Chinese sub Forum
Event monitoring problem