jdk1.8 list指定值去分组 使用jdk1.8实现将list根据指定的值去分组的操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

jdk1.8 list指定值去分组 使用jdk1.8实现将list根据指定的值去分组的操作

大宝vs   2021-03-16 我要评论
想了解使用jdk1.8实现将list根据指定的值去分组的操作的相关内容吗,大宝vs在本文为您仔细讲解jdk1.8 list指定值去分组的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:jdk1.8,list,指定值,去分组,下面大家一起来学习吧。

我就废话不多说了,大家还是直接看代码吧~

Map<String, List<CommentQuery>> commentList = list.stream().collect(Collectors.groupingBy(CommentQuery::getNewsId));
for (Map.Entry<String, List<CommentQuery>> entry : commentList.entrySet()) {
String key = HOT_LIST_KEY + entry.getKey();
if (entry.getValue().isEmpty()) {
stringRedisTemplate.opsForValue().set(key, "");
} else {
Gson gson = new Gson();
String json = gson.toJson(entry.getValue());
//先删除redis数据然后在插入新的牛评数据
stringRedisTemplate.delete(key);
stringRedisTemplate.opsForValue().set(key, json);
stringRedisTemplate.expire(key, 1, TimeUnit.DAYS);
}
}

补充知识:java8 list集合利用stream 根据元素的指定属性进行分组统计

需求是对指定集合的学生信息,根据班级分组统计每个班所有学生的凭证数量。

    List<HashMap<String, Object>> result = new ArrayList<>(); 
    List<HashMap<String, Object>> list = getList(countVo); 
    list.stream()
        .collect(Collectors.groupingBy(map -> map.get("className"), Collectors.toList()))
        .forEach((key, groupMap) -> {
            HashMap<String, Object> stringObjectHashMap = groupMap.stream().reduce((item1, item2) -> {
            Integer pzListNo = item1.get("pzListNo") == null ? 0 : (Integer) item1.get("pzListNo");
            Integer pzListNo2 = item2.get("pzListNo") == null ? 0 : (Integer) item2.get("pzListNo");
 
            item1.put("pzListNo", pzListNo + pzListNo2);
            return item1;
          }).get();
 
          result.add(stringObjectHashMap);
        }); 
 
    return result;

以上这篇使用jdk1.8实现将list根据指定的值去分组的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们