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();

7.根据列表中的某个String类型属性值进行截取后并收集

        List<String> collect = list.stream().map(SetMealItemBo::getName)
                .map(e -> e.split(","))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值