本文主要是介绍List对象转成Map,List对象转成Set,List对象转成List<参数>,List对象转成List<String>,JDK1.8流式处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目的:
List对象转成Map,List对象转成Set,List对象转成List<参数>,List对象转成List<String>,JDK1.8流式处理
JDK1.8操作,使用lambada表达式处理List
第一步: 数据库获取数据
List<TbmNewsPopularFeelings> tbmNewsPopularFeelingsList = tbmNewsPopFeeMapper.selectList(wrapper);
if (!CollectionUtils.isEmpty(tbmNewsPopularFeelingsList)) {
第二步,List<对象> 转成Map格式,List对象转成Map
Map<Long, TbmNewsPopularFeelings> resultMap = tbmNewsPopularFeelingsList.stream().collect(Collectors.toMap(TbmNewsPopularFeelings::getNewsId, v -> v, (k1, k2) -> k1));
第三步 List对象转成List<参数>
List<Long> newsIdLsit = tbmNewsPopularFeelingsList.stream().map(TbmNewsPopularFeelings::getNewsId).collect(Collectors.toList());
Set newsIdSet = new HashSet();
newsIdSet.addAll(newsIdLsit);
if (newsIdSet.size() == 1 && newsIdSet.contains(newsId)) {
return null;
} else {
newsIdSet.remove(newsId);
return newsIdSet.toString();
}
}
return null;
第四步,List可以通过group By来改造成一个Map
//Map<公司id,标签列表> 根据公司id分组
Map<String, List<TbmEventTags>> businessIdMap = tbmEventTagsList.stream().collect(Collectors.groupingBy(TbmEventTags::getBusinessId));
List<TbmNewsPopularFeelings> tbmNewsPopularFeelingsList = tbmNewsPopFeeMapper.selectList(wrapper);if (!CollectionUtils.isEmpty(tbmNewsPopularFeelingsList)) {Map<Long, TbmNewsPopularFeelings> resultMap = tbmNewsPopularFeelingsList.stream().collect(Collectors.toMap(TbmNewsPopularFeelings::getNewsId, v -> v, (k1, k2) -> k1));List<Long> newsIdLsit = tbmNewsPopularFeelingsList.stream().map(TbmNewsPopularFeelings::getNewsId).collect(Collectors.toList());Set newsIdSet = new HashSet();newsIdSet.addAll(newsIdLsit);if (newsIdSet.size() == 1 && newsIdSet.contains(newsId)) {return null;} else {newsIdSet.remove(newsId);return newsIdSet.toString();}}return null;
这篇关于List对象转成Map,List对象转成Set,List对象转成List<参数>,List对象转成List<String>,JDK1.8流式处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!