java8 stream操作集合

本文转自:https://blog.csdn.net/weixin_39800144/article/details/80336057

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

1.实体类要实现可比接口

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> list44= list4.stream()
                .sorted((map1, map2) -> map1.get("cnt").toString().compareTo(map2.get("cnt").toString())).collect(Collectors.toList());

5.根据列表中的地图的某个属性值进行过滤

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

6.根据列表中的地图的某个属性值进行求和

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

猜你喜欢

转载自blog.csdn.net/qq_40074764/article/details/80522105