今天就跟大家聊聊有关使用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); } }