java8Stream操作集合進行排序和過濾

2021-08-28 02:22:58 字數 697 閱讀 4598

//對listresult進行排序,根據伴隨度進行降序

listcollect = listresult.stream()

.sorted(comparator.comparing(followimsi::getfollowdegree).reversed())

.collect(collectors.tolist());

根據集合中物件followimsi中的伴隨度進行倒序排列...reversed(),預設正序,reversed反轉後即倒序;

listfirsta = listentity.stream()

.filter(collisionentity -> collisionentity.getmatchnums() >= 2)

.collect(collectors.tolist());

過濾,過濾掉collisionentity中匹配次數多於兩次的結果,firsta中存放的都是多於兩次的

listresult = firsta.stream()

.sorted(comparator.comparing(collisionentity::getmatchnums))

.collect(collectors.tolist());//根據matchnums排序

根據匹配次數正序排列,從小到大.

Java8 Stream 中間操作

1 filter 過濾,接收lambda,從流中排除某些元素 內部迭代 迭代操作有stream api完成 test public void test1 終止操作 一次性執行全部內容 stream.foreach system.out println 外部迭代 test public void te...

JAVA8 stream 流相關操作

1.查詢流中滿足條件的第乙個元素 集合 stream filter item 條件 findany get 集合 stream filter item 條件 findfirst get 2.內迴圈 集合 stream foreach item 3.將集合轉換為map function.identit...

Java8 Stream經典示例

示例一 class user public int getid public string tostring 現在有乙個list的集合,如何把這個list轉換成map其中,key是user id,value是user物件 如下 listusers arrays.aslist new user 1,t...