stream流的简单使用详解

导读:本篇文章讲解 stream流的简单使用详解,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  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)]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83558.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!