jdk1.8将list根据指定的值去分组的方法

Map> commentList = list.stream().collect(Collectors.groupingBy(CommentQuery::getNewsId));
for (Map.Entry> 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);
}
}

你可能感兴趣的:(Java,list)