Stream流

目录

1、定义

2、特点

3、创建

4、常用方法 

        4.1、遍历--forEach

        4.2、匹配--find/match     

        4.3、筛选--filter

        4.4、映射--map

        4.5、收集--collect

                4.5.1、归集

                4.5.2、统计

                4.5.3、分组

                4.5.4、接合

                4.5.5、归约

        4.6、排序--sorted

        4.7、提取/组合

                4.7.1、合并

                4.7.2、去重

                4.7.3、限制

                4.7.4、跳过


​​​​​​​​​​​​​​

1、定义

它能够将数组、集合转换成流,借助Stream API 对流中的元素进行操作,比如筛选、排序、聚合等。这种对流中数据的操作,类似于使用SQL执行的数据库查询。

2、特点

        1.1、流不存储元素:照特定的规则对数据进行计算,一般会输出结果

        1.2、流不会改变其数据源:通常情况下会产生一个新的集合或一个值

        1.3、流执行具有延迟特性:只有调用终端操作时,中间操作才会执行

3、创建


        // 方式一:数组方式创建
        String[] array = {"aa","bb"};
        Stream<String> arrayStream = Arrays.stream(array);
        arrayStream.forEach(System.out::println);

        // 方式二:集合方式创建
        List<String> list = Arrays.asList("a","b","c");
        // 创建普通流
        Stream<String> stream = list.stream();
        // 创建并行流(内部以多线程并行执行的方式对流进行操作,前提是流中的数据处理没有顺序要求)
        Stream<String> stringStream = list.parallelStream();

        // 创建方式三:使用Stream的静态方法 of()、iterate()、generate()
        Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 6);
        stream1.forEach(System.out::println);

        Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(3);
        stream2.forEach(System.out::println);

        Stream<Double> stream3 = Stream.generate(Math::random).limit(3);
        stream3.forEach(System.out::println);

4、常用方法 

        4.1、遍历--forEach

        // forEach()
        List<String> list = Arrays.asList("a", "b", "c");
        list.stream().forEach(System.out::println); // a b c

        4.2、匹配--find/match     

        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        // Optional类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象

        // findFirst() 匹配第一个 
        Optional<Integer> first = list.stream().findFirst();
        System.out.println(first.get()); // 7

        // findAny() 匹配任意(适用于并行流)
        // 如果数据量较少使用普通流方式一般返回第一个结果。如果是并行流的情况可能就是随机结果
        Optional<Integer> any = list.stream().findAny();
        System.out.println(any.get()); // 7

        // allMatch() 流中所有元素满足条件才返回true
        boolean allMatch = list.stream().allMatch(x -> x > 0);
        System.out.println(allMatch); // false

        // noneMatch() 流中所有元素都不符合才返回true
        boolean noneMatch = list.stream().noneMatch(x -> x < 0);
        System.out.println(noneMatch); // true

        // anyMatch()  流中只要有一个元素符合就返回true 
        boolean anyMatch = list.stream().anyMatch(x -> x > 3);
        System.out.println(anyMatch); // true

        4.3、筛选--filter

        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        // filter() 对原始Stream过滤,符合条件的留下生成新的流
        list.stream().filter(x -> x > 6).forEach(System.out::println); // 7 8 9

        4.4、映射--map

        // map() 接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
        String[] strArr = { "aa", "bb", "cc"};
        // Arrays.stream(strArr).map(x -> x.toUpperCase()).forEach(System.out::println); // 结果一样,写法不同
        Arrays.stream(strArr).map(String::toUpperCase).forEach(System.out::println); // AA BB CC

        // flatMap() 将流扁平化,接收一个函数作为参数,流中的每一个元素都被拆解成一个新的流。
        String[] words = {"ab", "cd"};
        Stream.of(words)
                .map(word -> word.split(""))
                .flatMap(Arrays::stream).forEach(System.out::println); // a b c d

        4.5、收集--collect

                4.5.1、归集

public class User {
    private String name;
    private int age;
} 

 public static void main(String[] args) {
        // collect 因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里
        List<Integer> list3 = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20);

        // toList()
        List<Integer> listNew3 = list3.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
        System.out.println(listNew3); // [6, 4, 6, 6, 20]

        List<User> userList = new ArrayList<>();
        userList.add(new User("zs",10));
        userList.add(new User("ls",20));
        List<String> collect = userList.stream().map(User::getName).collect(Collectors.toList());
        System.out.println(collect); //[zs, ls]

        // toSet()
        Set<Integer> set = list3.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
        System.out.println(set); // [4, 20, 6]

        // toMap()
        Map<?, User> map = userList.stream().filter(p -> p.getAge() > 18)
                .collect(Collectors.toMap(User::getName, p -> p));
        System.out.println(map); // {ls=User{name='ls', age=20}}
}

                4.5.2、统计

 public class User {
    private String name;
    private int age;
} 
 
 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("zs", 10));
        userList.add(new User("ls", 20));
        userList.add(new User("ww", 30));

        // counting() 计数
        Long addCollect = userList.stream().collect(Collectors.counting());
        // long count = userList.stream().count();
        System.out.println(addCollect); // 3

        // averagingInt/averagingLong/averagingDouble 平均值
        Double avgCollection = userList.stream().collect(Collectors.averagingInt(User::getAge));
        System.out.println(avgCollection); //20.0

        // maxBy()/minBy() 最值
        Optional<Integer> maxCollect = userList.stream().map(User::getAge).collect(Collectors.maxBy(Integer::compare));
        // Optional<Integer> maxCollect = userList.stream().map(User::getAge).max(Integer::compare);
        System.out.println(maxCollect.get()); //30

        // summingInt/summingLong/summingDouble 求和
        Integer sumCollection = userList.stream().collect(Collectors.summingInt(User::getAge));
        // Integer sumCollection = userList.stream().mapToInt(User::getAge).sum();
        System.out.println(sumCollection); //60

        // summarizingInt/summarizingLong/summarizingDouble 统计所有
        IntSummaryStatistics allCollect = userList.stream().collect(Collectors.summarizingInt(User::getAge));
        System.out.println(allCollect); //IntSummaryStatistics{count=3, sum=60, min=10, average=20.000000, max=30}
}

                4.5.3、分组

    public class User {
    private String name;
    private int age;
} 
 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("zs", 10));
        userList.add(new User("ls", 20));
        userList.add(new User("ww", 30));

        // partitioningBy/groupingBy   分组
        Map<Boolean, List<User>> partitionCollect = userList.stream().collect(Collectors.partitioningBy(User -> User.getAge() > 20));
        System.out.println(partitionCollect); // {false=[User{name='zs', age=10}, User{name='ls', age=20}], true=[User{name='ww', age=30}]}
        Map<Boolean, List<User>> groupCollect = userList.stream().collect(Collectors.groupingBy(User -> User.getAge() < 20));
        System.out.println(groupCollect); // {false=[User{name='ls', age=20}, User{name='ww', age=30}], true=[User{name='zs', age=10}]}
}

                4.5.4、接合

    public class User {
    private String name;
    private int age;
} 

 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("zs", 10));
        userList.add(new User("ls", 20));
        userList.add(new User("ww", 30));

        // joining 用特定的连接符(没有的话,则直接连接)连接成一个字符串。
 System.out.println(userList.stream().map(User::getName).collect(Collectors.joining(","))); //zs,ls,ww
}

                4.5.5、归约

    public class User {
    private String name;
    private int age;
} 
 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("zs", 10));
        userList.add(new User("ls", 20));
        userList.add(new User("ww", 30));

        // reduce/reducing 作用是把 Stream 元素组合起来。它提供一个起始值(种子),然后依照运算规则(BinaryOperator),和前面 Stream 的第一个、第二个、第 n 个元素组合
        // 每个用户年龄数加0减去2
        Integer reduceCollection = userList.stream().collect(Collectors.reducing(0, User::getAge, (i, j) -> (i + j - 2)));
        // userList.stream().map(User::getAge).reduce(0, (i, j) -> (i + j - 2));
        System.out.println(reduceCollection);

}

        4.6、排序--sorted

    public class User {
    private String name;
    private int age;
} 
 public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("zs", 10));
        userList.add(new User("ls", 20));
        userList.add(new User("ww", 30));

        // sorted(Comparator com) 排序
        List<User> sortedList = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
        sortedList.stream().forEach(e-> System.out.println(e.getAge())); // 32 25 20
}

        4.7、提取/组合

                4.7.1、合并

public static void main(String[] args) {
        // concat 合并
        Stream<String> stream1 = Stream.of("a","b","c","d");
        Stream<String> stream2 = Stream.of("a","b","c","d");
        List<String> concatCollect = Stream.concat(stream1, stream2).collect(Collectors.toList());
        System.out.println(concatCollect); // [a, b, c, d, a, b, c, d]
}

                4.7.2、去重

public static void main(String[] args) {
        // distinct 去重
        List<Integer> list = Arrays.asList(1,1,2,3,5,5,7);
        List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList());
        System.out.println(distinctList); //[1, 2, 3, 5, 7]]
}

                4.7.3、限制

public static void main(String[] args) {
        // limit 限制从流中获得前n个数据
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
        List<Integer> limitList = list.stream().limit(3).collect(Collectors.toList());
        System.out.println(limitList); //[1, 2, 3]
}

                4.7.4、跳过

public static void main(String[] args) {
        // skip 跳过前n个数据
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        List<Integer> skipList = list.stream().skip(2).collect(Collectors.toList());
        System.out.println(skipList); //3, 4, 5, 6, 7]
}

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值