Java8特性之根據stream優雅的統計、排序處理list數據

注:

List<ForumPlateVo> list = new ArrayList<>();
list.add.......忽略一萬字


ForumPlateVo屬性:clickNum(點擊量)、replyNum(回覆量)、newReplyTime(最新回覆時間)、postNum(帖子量)

1、計算總量

(int)list.stream().mapToInt(n -> n.getClickNum()).summaryStatistics().getSum()
(int)list.stream().mapToInt(n -> n.getReplyNum()).summaryStatistics().getSum()

2、獲取最近時間

long date = list.stream().filter(x -> x!=null).filter(y -> y.getNewReplyTime()!=null).mapToLong(n -> n.getNewReplyTime().getTime()).summaryStatistics().getMax();
if(date>0) forumPlateVo.setNewReplyTime(new Date(date));

 3、根據點擊量排序

list = list.stream().sorted(Comparator.comparing(ForumPlateVo::clickNum).reversed()).collect(Collectors.toList());

4、group by 獲取size

int count = list.stream().collect(groupingBy(ActivityLottery::getOpenId)).size();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章