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