java8的lambda過濾list遍歷集合,排序


1.根據屬性過濾list

List<AllManagerBean> testLists = broadCastRoomMapper.allManagerlist();

List<AllManagerBean> mans = testLists.stream().filter(j->j.getRoomId().equals(roomid)).collect(Collectors.toList());

 //過濾某一屬性,成一個新集合

 List<String> uids = testLists.stream().map(e->e.getUserid()).collect(Collectors.toList());

 

2.遍歷集合

List<ManagerBean> managerListNew = new ArrayList<ManagerBean>();

if (mans != null ){
mans.forEach(man->{
managerListNew.add(man);
});
}

3.根據某一集合對象中的某一屬性,排序

List<Model> thlistbysort = thlist.stream().sorted(Comparator.comparing(Model::getSort)).collect(Collectors.toList());                    //正序

List<Model> thlistbysort = thlist.stream().sorted(Comparator.comparing(Model::getSort).reversed()).collect(Collectors.toList());   //倒序

 

4.對List<String>進行正序,倒序排列

//正序排列
Collections.sort(s);
//倒序排列(先對list正序排列,然后反向排序)
Collections.sort(s);

Collections.reverse(s);//反向排序

 

 關於lanbda表達式相關基礎可參考

https://my.oschina.net/u/4006148/blog/3078359


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM