jdk8新特性Stream流

Stream流开始操作

通过集合创建

    /**
     * 通过集合创建
     */
    @Test
    public void test1(){
    
    
        List<String> stream1 = Arrays.asList("java", "h5", "python", "php", "c");
        stream1.stream().forEach(s -> System.out.println(s));
    }

通过数组创建

    /**
     *通过数组创建
     */
    @Test
    public void test2(){
    
    
        String [] str={
    
    "春","夏","球","冬"};
        Arrays.stream(str).forEach(System.out::println);
    }

通过Stream的of创建

    /**
     * 通过Stream的of创建
     */
    @Test
    public void test3(){
    
    
        Stream.of("小明","小王","小李","小杨").forEach(System.out::println);
    }

Stream流中间操作

filter过滤

 /**
     *stream过滤操作
     */
    @Test
    public void test1(){
    
    
        //创建stream
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

        //filter过滤操作
        stream = stream.filter(a -> a % 2 == 0);

        //输出保留偶数值
        stream.forEach(System.out::println);

        //创建字符串数据,过滤李四
        List<String> list = Arrays.asList("张三", "李四", "王五");
        Stream<String> name = list.stream().filter(str -> !"李四".equals(str));
        name.forEach(s-> System.out.println(s));

        list.stream().filter(s->!"王五".equals(s)).forEach(System.out::println);
    }

distinct去重

    /**
     *distinct去重操作
     */
    @Test
    public void test2(){
    
    
        Stream<Integer> stream = Stream.of(1, 1, 8, 2, 5, 4, 4, 2, 9, 8, 3);
        stream.distinct().forEach(System.out::println);
    }

limit截断,只保留前几位

    /**
     *limit截断操作
     */
    @Test
    public void test3(){
    
    
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
        stream.limit(5).forEach(System.out::println);
    }

skip跳过,跳过几位,与limit相反

    /**
     *skip跳过
     */
    @Test
    public void test4(){
    
    
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
        stream.skip(5).forEach(System.out::println);
    }

peek对每个元素进行Lambda操作

    /**
     *peek
     */
    @Test
    public void test5(){
    
    
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
        stream.peek(c->System.out.println(c)).count();
    }

sorted排序按自然顺序排序

    /**
     *sorted自然排序
     */
    @Test
    public void test6(){
    
    
        Stream<Integer> stream = Stream.of(11, 23, 53, 4, 4, 55, 76, 7, 83, 29, 60);
        stream.distinct().sorted().forEach(System.out::println);
    }

map映射成新元素

    /**
     *map映射
     */
    @Test
    public void test7(){
    
    
        Stream<Integer> stream = Stream.of(11, 23, 53, 4, 4, 55, 76, 7, 83, 29, 60);
        stream.distinct().map(m->m+1).forEach(System.out::println);

        Stream<String> list = Stream.of("mi", "jd", "taobao", "baidu");
        list.map(t->t.toUpperCase()).forEach(System.out::println);
    }

所有中间操作方法列表

方 法 描 述
filter(Predicate p) 接收 Lambda , 从流中排除某些元素
distinct() 筛选,通过流所生成元素的equals() 去除重复元素
limit(long maxSize) 截断流,使其元素不超过给定数量
skip(long n) 跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补
peek(Consumer action) 接收Lambda,对流中的每个数据执行Lambda体操作
sorted() 产生一个新流,其中按自然顺序排序
sorted(Comparator com) 产生一个新流,其中按比较器顺序排序
map(Function f) 接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
mapToDouble(ToDoubleFunction f) 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 DoubleStream。
mapToInt(ToIntFunction f) 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 IntStream。
mapToLong(ToLongFunction f) 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 LongStream。
flatMap(Function f) 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流

Stream终结操作

forEach迭代遍历

count返回流中元素总数

allMatch检查是否匹配所有元素

    /**
     * allMatch检查是否匹配所有元素
     */
    @Test
    public void test1(){
    
    
        boolean b = Stream.of(2, 4, 6, 8).allMatch(t -> t % 2 == 0);
        System.out.println(b);
    }

anyMatch检查是否至少匹配一个元素

    /**
     * anyMatch检查是否至少匹配一个元素
     */
    @Test
    public void test2(){
    
    
        boolean b = Stream.of(2, 3, 7, 5).anyMatch(t -> t % 2 == 0);
        System.out.println(b);
    }

findFirst返回第一个元素

    /**
     * findFirst返回第一个元素
     */
    @Test
    public void test3(){
    
    
        Optional<Integer> first = Stream.of(2, 3, 7, 5).findFirst();
        System.out.println(first.get());
    }

max返回最大值

    /**
     * max返回最大值
     */
    @Test
    public void test4(){
    
    
        Optional<Integer> max = Stream.of(2, 3, 7, 5).max(Integer::compareTo);
        System.out.println(max.get());
    }

reduce可以将流中元素反复结合操作起来,得到一个值

    /**
     * reduce
     */
    @Test
    public void test5(){
    
    
        String reduce = Stream.of("a", "b", "c", "d").reduce("hhh", String::concat);
        System.out.println("recoed:"+reduce);
    }

collect将流转换为其他形式

    /**
     * collect
     */
    @Test
    public void test6(){
    
    
        List<String> collect = Stream.of("a", "b", "c", "d").collect(Collectors.toList());
        collect.forEach(System.out::println);
    }

所有总结操作的方法列表

方法 描述
boolean allMatch(Predicate p) 检查是否匹配所有元素
boolean anyMatch(Predicate p) 检查是否至少匹配一个元素
boolean noneMatch(Predicate p) 检查是否没有匹配所有元素
Optional findFirst() 返回第一个元素
Optional findAny() 返回当前流中的任意元素
long count() 返回流中元素总数
Optional max(Comparator c) 返回流中最大值
Optional min(Comparator c) 返回流中最小值
void forEach(Consumer c) 迭代
T reduce(T iden, BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 T
U reduce(BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 Optional
R collect(Collector c) 将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法

猜你喜欢

转载自blog.csdn.net/weixin_44226883/article/details/123899439