当前位置:网站首页>List转Map的几种方式
List转Map的几种方式
2022-08-03 00:56:00 【道阻且长-行则将至-行而不辍-未来可期】
前言需求:
需要记录下之前List的转Map的使用方法,感觉好记性不如烂笔头,记录下之前使用过的List转成Map的实现java逻辑代码
RuleExpress ruleExpress = new RuleExpress();
ruleExpress.setNewsType(1);
List<RuleExpress> commonRuleExpressList = ruleExpressDao.listRuleExpresss(ruleExpress);
Map<String, List<RuleExpress>> commonRuleExpressMap = commonRuleExpressList.stream().collect(Collectors.groupingBy(RuleExpress::getType, LinkedHashMap::new, Collectors.toList()));
ContantData.setCommonNewsRuleExpressMap(commonRuleExpressMap);
ruleExpress.setNewsType(2);
List<RuleExpress> fastNewsRuleExpressList = ruleExpressDao.listRuleExpresss(ruleExpress);
Map<String, List<RuleExpress>> fastNewsRuleExpressMap = fastNewsRuleExpressList.stream().collect(Collectors.groupingBy(RuleExpress::getType, LinkedHashMap::new, Collectors.toList()));
ContantData.setFastNewsRuleExpressMap(fastNewsRuleExpressMap);
Map<Integer, List<Student>> commonMap = list.stream().collect(Collectors.groupingBy(Student::getNo, LinkedHashMap::new, Collectors.toList()));
Student noMax = list.stream().max(Comparator.comparing(Student::getNo)).get();
List<Student> maxList=commonMap.get(noMax.getNo());
System.out.println("------最大的列表---------");
System.out.println(maxList.toString());
System.out.println("---------------");
@Override
public Map<Integer, Map<String, String>> getNewsRangeConfig() {
QueryWrapper<TbmNewsRangeConfig> queryWrapper = new QueryWrapper();
queryWrapper.orderByAsc("id");
List<TbmNewsRangeConfig> tbmNewsRangeConfigList = tbmNewsRangeConfigMapper.selectList(queryWrapper);
Map firstLevelCodeMap = tbmNewsRangeConfigList.stream().collect(Collectors.groupingBy(TbmNewsRangeConfig::getFirstLevelCode, LinkedHashMap::new, Collectors.toMap(TbmNewsRangeConfig::getFirstLevelCode, TbmNewsRangeConfig::getFirstLevelName, (k1, k2) -> k1)));
Map secondLevelCodeMap = tbmNewsRangeConfigList.stream().collect(Collectors.groupingBy(TbmNewsRangeConfig::getSecondLevelCode, LinkedHashMap::new, Collectors.toMap(TbmNewsRangeConfig::getSecondLevelCode, TbmNewsRangeConfig::getSecondLevelName, (k1, k2) -> k1)));
Map thirdLevelCodeMap = tbmNewsRangeConfigList.stream().collect(Collectors.groupingBy(TbmNewsRangeConfig::getThirdLevelCode, LinkedHashMap::new, Collectors.toMap(TbmNewsRangeConfig::getThirdLevelCode, TbmNewsRangeConfig::getThirdLevelName, (k1, k2) -> k1)));
Map fourthLevelCodeMap = tbmNewsRangeConfigList.stream().collect(Collectors.groupingBy(TbmNewsRangeConfig::getFourthLevelCode, LinkedHashMap::new, Collectors.toMap(TbmNewsRangeConfig::getFourthLevelCode, TbmNewsRangeConfig::getFourthLevelName, (k1, k2) -> k1)));
Map<Integer, Map<String, String>> result = new LinkedHashMap<>();
result.put(1, firstLevelCodeMap);
result.put(2, secondLevelCodeMap);
result.put(3, thirdLevelCodeMap);
result.put(4, fourthLevelCodeMap);
return result;
}
边栏推荐
- UVM中SVA使用指南
- 阿里云增强版实人认证--银行卡要素核验
- 10大领域5大过程47子过程快速记忆
- 中科磁业IPO过会:年营收5.5亿 吴中平家族持股85%
- Last Common Ancestor (LCA) Study Notes | P3379 【Template】Least Common Ancestor (LCA) Problem Solution
- 1686. 石子游戏 VI
- Greenplum database failure analysis, can not listen to the port
- 面试题 08.07. 无重复字符串的排列组合 ●●
- The Sandbox 市场平台将上线 Isla Obscura 第五期 NFT 作品集
- 全栈----跨域
猜你喜欢
随机推荐
「PHP基础知识」隐式数据类型
11-security认证.md
【飞控开发高级教程1】疯壳·开源编队无人机-飞控整机代码走读、编译与烧写
阿南的对话
企业云成本管控,你真的做对了吗?
[NCTF2019]SQLi-1||SQL注入
可信的SSL证书颁发机构有哪些
VS Code 这么牛,再次印证了一句名言
7.31
鲲鹏devkit开发套件
php一维数组合并
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
怎么做postgrsql主备?
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
阿里云增强版实人认证--银行卡要素核验
一个接口并发问题的模拟与复现
风电场运营实践 | 麒麟信安助力国华投资山东公司集控中心实现安全智慧化运营
全栈----跨域
【Swoole系列3.3】单进程管理Process
pytest:如何调用 pytest