当前位置:网站首页>list转换map(根据key来拆分list,相同key的value为一个list)
list转换map(根据key来拆分list,相同key的value为一个list)
2020-11-06 21:45:00 【会开车的架构师】
/**
* @description 按照key拆分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);
}
}
版权声明
本文为[会开车的架构师]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4629483/blog/4529695
边栏推荐
- C#和C/C++混合编程系列5-内存管理之GC协同
- Vue communication and cross component listening state Vue communication
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- 面试官: ShardingSphere 学一下吧
- 给字节的学姐讲如何准备“系统设计面试”
- 視覺滾動[反差美]
- An article will take you to understand CSS alignment
- 意外的元素..所需元素..
- Gather in Beijing! The countdown to openi 2020
- Markdown tricks
猜你喜欢

Git rebase is in trouble. What to do? Waiting line

git远程库回退指定版本

What are PLC Analog input and digital input

Pn8162 20W PD fast charging chip, PD fast charging charger scheme

ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录

Gather in Beijing! The countdown to openi 2020

Multi robot market share solution

理解格式化原理

Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform

Contract trading system development | construction of smart contract trading platform
随机推荐
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
C語言I部落格作業03
An article takes you to understand CSS pagination examples
What is alicloud's experience of sweeping goods for 100 yuan?
只有1个字节的文件实际占用多少磁盘空间
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
What knowledge do Python automated testing learn?
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
ERD-ONLINE 免费在线数据库建模工具
What are PLC Analog input and digital input
解决 WPF 绑定集合后数据变动界面却不更新的问题
The importance of big data application is reflected in all aspects
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
Vue communication and cross component listening state Vue communication
ado.net和asp.net的关系
ES6 learning notes (4): easy to understand the new grammar of ES6
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
electron 實現檔案下載管理器