关于Stream的使用-个人笔记

什么是 Stream?

参考资料: 

在 Java 8 中, 集合接口有两个方法来生成流:
stream() − 为集合创建串行流。
parallelStream() − 为集合创建并行流。

常用方法:

.stream()
stream()把一个源数据,可以是集合,数组,I/O channel, 产生器generator 等,转化成流

.filter()
filter()方法用于通过设置的条件过滤出元素

.sorted()
sorted()用于对流进行排序

.map()
map()用于映射每个元素到对应的结果

.collect()
Collectors类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串,collect()内可以用collectors进行转换:

.collect(Collectors.toList());//列表
.collect(Collectors.joining(", "));//字符串
.findFirst()
findFirst()用于找到第一次出现的元素

.isPresent()
isPresent()可以判断所找到的值是否是null

        List<String> list1 = new ArrayList<String>() {{
            add("apple");
            add("banana");
            add("pear");
            add("orange");
        }};

        //遍历操作(map):使用map操作可以遍历集合中的每个对象,并对其进行操作,map之后,用.collect(Collectors.toList())会得到操作后的集合
        List<String> newList2 = list1.stream().map(m -> m.substring(0, 3)).collect(Collectors.toList());
        System.out.println(newList2);
        List<String> newList3 = list1.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(newList3);

        //求平方
        List<Integer> nums = Arrays.asList(1, 2, 3, 4);
        List<Integer> newNums = nums.stream().map(m -> m * m).collect(Collectors.toList());
        System.out.println(newNums);

        //过滤空
        List<String> filterLists = new ArrayList<>();
        filterLists.add("");
        filterLists.add("a");
        filterLists.add("b");
        List<String> newfilterLists = filterLists.stream().filter(p -> !p.isEmpty()).collect(Collectors.toList());
        System.out.println(newfilterLists);

        //遍历
        List<String> forEachLists = new ArrayList<>();
        forEachLists.add("a");
        forEachLists.add("b");
        forEachLists.add("c");
        forEachLists.add("d");
        forEachLists.add("e");
        forEachLists.add("e");
        forEachLists.add("f");
        forEachLists.stream().forEach(s -> System.out.println(s));
        //limit 返回 Stream 的前面 n 个元素;skip 则是扔掉前 n 个元素
        List<String> newforEachLists1 = forEachLists.stream().limit(4).collect(Collectors.toList());
        List<String> newforEachLists2 = forEachLists.stream().skip(4).collect(Collectors.toList());
        System.out.println(forEachLists);
        System.out.println(newforEachLists1);
        System.out.println(newforEachLists2);

        //排序(sort/min/max/distinct)
        List<String> newforEachLists4 = forEachLists.stream().distinct().collect(Collectors.toList());
        System.out.println(newforEachLists4);
        List<Integer> sortedLists = new ArrayList<>();
        sortedLists.add(1);
        sortedLists.add(2);
        sortedLists.add(6);
        sortedLists.add(4);
        sortedLists.add(3);
        sortedLists.add(5);
        List<Integer> sortedLists1 = sortedLists.stream().sorted().collect(Collectors.toList());
        int max = sortedLists.stream().mapToInt(m -> m).max().getAsInt();
        int min = sortedLists.stream().mapToInt(m -> m).min().getAsInt();
        System.out.println(sortedLists1);
        System.out.println(max + "----" + min);

        //match匹配:allMatch(所有元素符合)、anyMatch(只要有一个元素符合)、noneMatch(没有任何一个元素符合)
        boolean b = filterLists.stream().anyMatch(p -> p.isEmpty());
        System.out.println(b);



        List<User> userList = null;//模拟对象集合
        //按部门分组
        Map<String, List<String>> collect = userList.stream().filter(p -> !p.getDept().isEmpty()).collect(Collectors.groupingBy(x -> x.getDept()));

        //求总分数
        Integer total = userList.stream().mapToInt(e -> Integer.parseInt(e.getScore().toString())).sum();


        //并行:获取不为空串的数量
        long count = filterLists.parallelStream().filter(p -> !p.isEmpty()).count();
        System.out.println(count);


版权声明:本文为weixin_39516640原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。