当前位置:网站首页>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
边栏推荐
- Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
- ERD-ONLINE 免费在线数据库建模工具
- 一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
- Unity性能优化整理
- Network programming NiO: Bio and NiO
- Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
- What is alicloud's experience of sweeping goods for 100 yuan?
- WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
- (2) ASP.NET Core3.1 Ocelot routing
- Vue communication and cross component listening state Vue communication
猜你喜欢
StickEngine-架构11-消息队列(MessageQueue)
How to understand Python iterators and generators?
只有1个字节的文件实际占用多少磁盘空间
How to hide part of barcode text in barcode generation software
What are manufacturing and new automation technologies?
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
递归、回溯算法常用数学基础公式
ado.net和asp.net的关系
Use modelarts quickly, zero base white can also play AI!
随机推荐
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
Small program introduction to proficient (2): understand the four important files of small program development
python100例項
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
Multi robot market share solution
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
前端未來趨勢之原生API:Web Components
Python basic data type -- tuple analysis
Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
git远程库回退指定版本
游戏主题音乐对游戏的作用
只有1个字节的文件实际占用多少磁盘空间
開源一套極簡的前後端分離專案腳手架
CloudQuery V1.2.0 版本发布
统计项目代码行数
解决 WPF 绑定集合后数据变动界面却不更新的问题
What are the common problems of DTU connection
【学习】接口测试用例编写和测试关注点
ES6 learning notes (2): teach you to play with class inheritance and class objects