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);
}
}
jdk1.8将list根据指定的值去分组的方法
最新推荐文章于 2022-09-21 22:27:59 发布