美文网首页Java8
java8中的Stream操作

java8中的Stream操作

作者: 虫儿飞ZLEI | 来源:发表于2019-04-13 18:25 被阅读0次

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

1.Stream的三个操作步骤

  • 创建stream

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

  • 终止操作(惰性求值)

2. 创建Stream的方式

2.1 调用集合的stream方法

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

2.2 调用Arrays的stream方法

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

2.3 Stream的静态of方法

       Stream<People> perples1 = Stream.of(perples);

2.4 创建无限流

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

3. 中间操作

3.1 Filter

        List<People> 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<People> peopleStream = peoples.stream()
                .filter((p) -> p.getI()>2);

        peopleStream.forEach((p)->System.out.println(p.getS()));
    }

3.2 limit

        List<People> 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<People> peopleStream = peoples.stream()
            .limit(2);
        peopleStream.forEach((p)->System.out.println(p.getS()));

3.3 skip 跳过前几个

        List<People> 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<People> peopleStream = peoples.stream()
            .skip(2);
        peopleStream.forEach((p)->System.out.println(p.getS()));

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

        List<People> 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<People> peopleStream = peoples.stream()
            .distinct();
        peopleStream.forEach((p)->System.out.println(p.getS()));

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

        List<People> 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<People> peopleStream = peoples.stream()
            .map(people -> {
                //接收一个任意参数,返回一个任意参数,基本上可以做任何操作了
                people.setS(people.getS().toUpperCase());
                return people;
            });
        peopleStream.forEach((p)->System.out.println(p.getS()));

3.5 sort 排序

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

        Stream<Integer> intstream = ints.stream()
                .sorted();

        intstream.forEach((s)->System.out.println(s));
  • 自己写排序
        List<People> 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()));

4. 终止操作

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



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



返回过一个处理过的集合

        List<People> 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<People> list = peoples.stream().limit(2).collect(Collectors.toList());

相关文章

  • JAVA8中Stream学习

    JAVA8中Stream学习 最近看了下Stream的api: Stream用户操作Collection族的数据,...

  • Java8 Stream 流的重用

    Java8 Stream 已经被操作或关闭 引言 在 java8 中,Stream 不能被重用,一旦它被使用或使用...

  • java8 stream lambda

    记录Java8的stream操作,供自己复习。 创建Stream Employee类 创建stream方法 for...

  • java 函数式编程 之 Stream API (二)

    在上一篇文章中,我们介绍了java8 中 stream API 的简单使用,其中包括对于stream流操作的相关方...

  • Java8 Stream API

    Stream API是Java8中处理集合的关键组件,提供了各种丰富的函数式操作。 Stream的创建 任何集合都...

  • Java8 Stream 使用及其详解

    一、什么是Stream 二、Java7和Java8 聚合操作的区别。 三、Stream 总览 四、Stream 的...

  • Java8 Stream终端操作使用详解

    前情回顾 前几篇讲了Java8中Stream流的简介,创建流的方式,以及Stream流中间操作的使用详解,想回顾的...

  • Stream基础知识

    Stream API Stream是Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,但是将执...

  • JAVA8 Stream接口,map操作,filter操作,fl

    这篇,我们来看Stream的一些中间操作,关于中间操作的一些介绍,可以看《JAVA8 stream接口 中间操作和...

  • java8 Stream类常用方法总结

    Java8中提供了Stream对集合操作作出了极大的简化,学习了Stream之后,我们以后不用使用for循环就能对...

网友评论

    本文标题:java8中的Stream操作

    本文链接:https://www.haomeiwen.com/subject/haxwiqtx.html