java8 集合操作

1.根据集合的某个属性值,给集合做排序.

1.实体类要实现Comparable接口

public class IndustryInfo implements Comparable<IndustryInfo>{

    private String code;

    private String name;

    private Integer num;

    private String str;
    
    @Override
    public int compareTo(IndustryInfo o) {
        return 0;
    }
    //get set 省略
}

2.按照num逆序排列

        List<IndustryInfo> listRe = list.stream().sorted(Comparator.comparing(IndustryInfo::getNum).reversed()).collect(Collectors.toList());

2.排除某个特定元素

List<Map<String, Object>> list8 = list7.stream().filter(map -> !map.get("name").toString().equals("高新技术企业")).collect(Collectors.toList());

3.获取某个特定元素

        Map<String, Object> map1 = list2.stream().filter(map -> map.get("name").toString().equals("黑榜企业")).collect(Collectors.toList()).get(0);

4.根据list中map的某个属性值排序list

        List<Map> list44= list4.stream()
                .sorted((map1, map2) -> map1.get("cnt").toString().compareTo(map2.get("cnt").toString())).collect(Collectors.toList());

5.根据list中map的某个属性值进行过滤

       List<Map> collect1 = list6.stream().filter(map -> map.get("entStatus").toString().indexOf("在营") != -1).collect(Collectors.toList());

6.根据list中map的某个属性值进行求和

      int sumZaiYing = collect1.stream().mapToInt(map -> Integer.valueOf(map.get("cnt").toString())).sum();

7.根据map的某个value来分类List<Map<String,Object>>

Map<Object, List<Map<String, Object>>> age = list.stream().collect(Collectors.groupingBy(map -> map.get("age")));

8.根据对象的某个属性值来分类List

Map<String, List<User>> collect1 = list2.stream().collect(Collectors.groupingBy(User::getProvince));
    原文作者:IT云清
    原文地址: https://blog.csdn.net/weixin_39800144/article/details/80336057#t6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞