stream流的简单使用详解

  1. // filter 过滤
        List<Integer> integerList = Arrays.asList(1, 2, 3,3, 4, 5, 6);
        //过滤出大于4的数据
        List<Integer> collect = integerList.stream().filter(e -> e > 4).collect(Collectors.toList());
System.out.println(collect); //[5, 6]
  1. //distinct 去重复
  List<Integer> collect1 = integerList.stream().distinct().collect(Collectors.toList());
        System.out.println(collect1);//[1, 2, 3, 4, 5, 6]
  1. //分组 groupingBy
  List<StoreUser> data1 = storeClient.getUserIdBySysInfo(leaveDetailDto.getSystemNames()).getData();
    Map<String, List<StoreUser>> collectByUser = data1.stream().collect(Collectors.groupingBy(StoreUser::getUserId)); //根据userId分组```

//分组之后我们得到map集合
//keySet()获取所有的键
List<String>   userIdsBySys = collectByUser.keySet().stream().map(e -> {
                    return e;
                }).collect(Collectors.toList());
  1. map 映射属性值
map映射方式
List<String> strings = Arrays.asList(details.get(0).getStoreId().split(","));
                List<String> ids = strings.stream().map(e -> {
                    R<StoreInfo> storeInfoR = storeClient.storeInfo(e);
                    return e + "-" + storeInfoR.getData().getStoreName();
                }).collect(Collectors.toList());//ids就是一个x+"-"+x这种数据格式
foreach方式
  strings.forEach(e->{
                    List <String> strings1 = new ArrayList<>();
                    strings1.add(e+"-"+storeClient.storeInfo(e).getData().getStoreName());
                });//strings1就是一个x+"-"+x这种数据格式
  1. //stream流方式排序 也是面试中较常问到的
  String[] strArr = { "ab", "bcdd", "defde", "ftr" };
      // 按照字符串的长度 长度 从小到大  升序
        List<String> collect1 = Arrays.stream(strArr).sorted(Comparator.comparing(String::length)).collect(Collectors.toList());
        System.out.println(collect1); //[ab, ftr, bcdd, defde]

//根据对象里的某个属性排序  默认是升序   reversed()降序
        List<WhseStoreVo> collect1 = whseStore.stream().sorted((Comparator.comparing(WhseStoreVo::getUpdateTime)).reversed()).collect(Collectors.toList());

  1. //分页
    List<WhseStoreVo> whseStore = iWhseStoreServicel.getWhseStore(param);
        List<WhseStoreVo> collect1 = whseStore.stream().sorted((Comparator.comparing(WhseStoreVo::getUpdateTime)).reversed()).collect(Collectors.toList());
        List<WhseStoreVo> collect = collect1.stream().skip((long) (Integer.parseInt(param.get("current").toString()) - 1) * Integer.parseInt(param.get("size").toString())).limit(Integer.parseInt(param.get("size").toString())).collect(Collectors.toList());
        result.setRecords(collect);
        result.setTotal(whseStore.size());
        return R.data(result);

collect
7. //collect操作可以接受各种方法作为参数,将流中的元素汇集,收集起来

//以下摘自面试题选集 
ArrayList<Person> personList = new ArrayList<>();
        personList.add(new Person("xiao",12));
        personList.add(new Person("xiao",20));
        personList.add(new Person("xiao",18));
        // 获取平均年龄 averaging
        Double collect = personList.stream().collect(Collectors.averagingInt(Person::getAge));
        System.out.println(collect); //16.666666666666668

        // summarizing
        DoubleSummaryStatistics collect1 = personList.stream().collect(Collectors.summarizingDouble(Person::getAge));
        System.out.println(collect1); // DoubleSummaryStatistics{count=3, sum=50.000000, min=12.000000, average=16.666667, max=20.000000}

        //joining
        String collect2 = personList.stream().map(p -> p.getName()).collect(Collectors.joining(","));
        System.out.println(collect2);  //xiao,xiao,xiao

        // reduce
        Integer collect3 = personList.stream().collect(Collectors.reducing(0, Person::getAge, (x, y) -> x + y));
        Optional<Integer> reduce = personList.stream().map(Person::getAge).reduce(Integer::sum);
        System.out.println(collect3); //50
        System.out.println(reduce);   //Optional[50]

        // groupingBy
        // 以名字进行分组
        Map<String, List<Person>> collect4 = personList.stream().collect(Collectors.groupingBy(Person::getName));
        System.out.println(collect4); //{xiao=[Person(name=xiao, age=12), Person(name=xiao, age=20), Person(name=xiao, age=18)]}
        // 先以名字分组,再以年龄分组
        Map<String, Map<Integer, List<Person>>> collect5 = personList.stream().collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(Person::getAge)));
        System.out.println(collect5); //{xiao={18=[Person(name=xiao, age=18)], 20=[Person(name=xiao, age=20)], 12=[Person(name=xiao, age=12)]}}

        // toList、toSet、toMap
        Set<Person> collect6 = personList.stream().collect(Collectors.toSet());
        System.out.println(collect6);//[Person(name=xiao, age=18), Person(name=xiao, age=20), Person(name=xiao, age=12)]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值