java8中的Stream操作

就是操作集合,java代码一样可以实现,只是stream写起来方便些

1.Stream的三个操作步骤

  • 创建stream

  • 中间操作(并不会操作,只有终止操作了以后,中间操作的代码才会执行)

  • 终止操作(惰性求值)

2. 创建Stream的方式

2.1 调用集合的stream方法

        ArrayList list = new ArrayList<>();
        Stream stream = list.stream();

2.2 调用Arrays的stream方法

        People[] perples = new People[10];
        Stream stream1 = Arrays.stream(perples);

2.3 Stream的静态of方法

       Stream perples1 = Stream.of(perples);

2.4 创建无限流

        //迭代
        Stream iterate = Stream.iterate(0, (x) -> x + 2);
        iterate.limit(19).forEach(System.out::println);
        //生成
        Stream generate = Stream.generate(Math::random);
        generate.limit(5).forEach(System.out::println);

3. 中间操作

3.1 Filter

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang2", 2, 2),
                new People("zhang3", 3, 3),
                new People("zhang4", 4, 4),
                new People("zhang5", 5, 5)
        );

        Stream peopleStream = peoples.stream()
                .filter((p) -> p.getI()>2);

        peopleStream.forEach((p)->System.out.println(p.getS()));
    }
java8中的Stream操作_第1张图片

3.2 limit

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang2", 2, 2),
                new People("zhang3", 3, 3),
                new People("zhang4", 4, 4),
                new People("zhang5", 5, 5)
        );

        Stream peopleStream = peoples.stream()
            .limit(2);
        peopleStream.forEach((p)->System.out.println(p.getS()));
java8中的Stream操作_第2张图片

3.3 skip 跳过前几个

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang2", 2, 2),
                new People("zhang3", 3, 3),
                new People("zhang4", 4, 4),
                new People("zhang5", 5, 5)
        );

        Stream peopleStream = peoples.stream()
            .skip(2);
        peopleStream.forEach((p)->System.out.println(p.getS()));
java8中的Stream操作_第3张图片

3.4 distinct去重(People类必须实现hashcode和equals方法才可以去重)

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang2", 2, 2),
                new People("zhang3", 3, 3),
                new People("zhang4", 4, 4),
                new People("zhang5", 5, 5),
                new People("zhang5", 5, 5),
                new People("zhang5", 5, 5)
        );

        Stream peopleStream = peoples.stream()
            .distinct();
        peopleStream.forEach((p)->System.out.println(p.getS()));
java8中的Stream操作_第4张图片

3.4 map 在map中可以通过代码实现上述Filter、distinct、skip、limit等等的操作

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang2", 2, 2),
                new People("zhang3", 3, 3),
                new People("zhang4", 4, 4),
                new People("zhang5", 5, 5)
        );

        Stream peopleStream = peoples.stream()
            .map(people -> {
                //接收一个任意参数,返回一个任意参数,基本上可以做任何操作了
                people.setS(people.getS().toUpperCase());
                return people;
            });
        peopleStream.forEach((p)->System.out.println(p.getS()));
java8中的Stream操作_第5张图片

3.5 sort 排序

  • 自然排序
        List ints = Arrays.asList(
                1,2,3,4,5,6,7
        );

        Stream intstream = ints.stream()
                .sorted();

        intstream.forEach((s)->System.out.println(s));
java8中的Stream操作_第6张图片
  • 自己写排序
        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang3", 3, 3),
                new People("zhang2", 2, 2),
                new People("zhang5", 5, 5),
                new People("zhang4", 4, 4)
        );

        peoples.stream().sorted((p1,p2)->{
            return p1.getI()-p2.getI();
        }).forEach(people -> System.out.println(people.getS()));
java8中的Stream操作_第7张图片

4. 终止操作

只有执行了终止操作,中间操作的代码才会执行

java8中的Stream操作_第8张图片


比如可以把所有people的某个int字段加起来



返回过一个处理过的集合

        List peoples = Arrays.asList(
                new People("zhang1", 1, 1),
                new People("zhang3", 3, 3),
                new People("zhang2", 2, 2),
                new People("zhang5", 5, 5),
                new People("zhang4", 4, 4)
        );

        List list = peoples.stream().limit(2).collect(Collectors.toList());

你可能感兴趣的:(java8中的Stream操作)