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进行正序,倒序排列

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

关于lanbda表达式相关基础可参考

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

猜你喜欢

转载自blog.csdn.net/heqiushuang110/article/details/126695727