Stream API 例子

Java8 中Stream类中源码方法例子


package com.h3c;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream.Builder;

public class Stream {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "bb", "ccc", "dddd", "eeeee", "ffffff");
        List<String> list1 = Arrays.asList("a", "bb", "ccc", "a", "bb", "ccc");
        List<String> list2 = Arrays.asList("dd", "da", "ac", "cc", "ee", "ea");

        /**
         * filter 过滤   length()>=2 返回值是Stream<String>类型
         */
        List<String> collect1 = list.stream().filter(s -> s.length() >= 2).collect(Collectors.toList());
        System.out.println(collect1);

        /**
         * map 转换    [ length()>=2 ] 返回值为 Stream<Boolean>
         */
        List<Integer> collect2 = list.stream().map(s -> s.length()).collect(Collectors.toList());
        System.out.println(collect2);

        /**
         * mapToInt 转换方法结果为int  返回值是IntStream类型
         * 同样的方法  mapToLong mapToDouble
         */
        int[] array = list.stream().mapToInt(s -> s.length()).toArray();
        double mapToInt = list.stream().mapToInt(s -> s.length()).average().getAsDouble();
        System.out.println(array.length);
        System.out.println(mapToInt);

        /**
         * flatMap 转换方法 结果必须为stream   返回值是stream
         */
        List<String> collect3 = list.stream().flatMap(s -> list.stream()).collect(Collectors.toList());
        System.out.println(collect3);

        /**
         * flatMapToInt 转换方法结果为 IntStream  返回值是IntStream
         * 类似 flatMapToLong flatMapToDouble
         */
        int[] array1 = list.stream().flatMapToInt(s -> list.stream().mapToInt(ss -> ss.length())).toArray();
        System.out.println(array1.length);

        /**
         * distinct 去重    和sql中distinct一样  返回值为Stream
         */
        Object[] array2 = list1.stream().distinct().toArray();
        System.out.println(array2.length);

        /**
         * sorted 排序方法   对list进行排序
         * java8 提供直接java.util.List中的sort方法,而不是旧的Collections.sort
         */
        //实例方法的引用和Comparator.comparing
        Collections.sort(list2, Comparator.comparing(String::toString));
        List<String> collect4 = list2.stream().sorted().collect(Collectors.toList());
        System.out.println(collect4);
        //排序反转  reversed()
        Comparator<String> comparator = (h1, h2) -> h1.compareTo(h2);
        list2.sort(comparator);
        list2.sort(comparator.reversed());
        System.out.println(list2);

        /**
         * sorted(Comparator<? super T> comparator) 排序方法
         */
        Comparator<String> comparator1 = (h1, h2) -> h1.compareTo(h2);
        List<String> collect5 = list2.stream().sorted(comparator1.reversed()).collect(Collectors.toList());
        System.out.println(collect5);

        /**
         * peek 用来<偷看一眼>的方法     不会改变list
         */
        List<String> collect6 = list.stream().peek(string -> System.out.println("偷看一眼" + string)).collect(Collectors.toList());
        List<String> collect7 = list.stream().peek(string -> string.concat("!")).map(string -> string.concat("!")).collect(Collectors.toList());
        System.out.println(collect6);
        System.out.println(collect7);

        /**
         * limit 限制多少个
         */
        List<String> collect8 = list.stream().limit(4).collect(Collectors.toList());
        System.out.println(collect8);

        /**
         * skip 跳过
         */
        List<String> collect = list.stream().skip(2).collect(Collectors.toList());
        System.out.println(collect);

        /**
         * forEach  循环  终止语句,返回值void
         */
        list.stream().forEach(System.out::println);
        list.forEach(System.out::println);

        /**
         * forEachOrdered  排序循环   终止语句,返回值void
         */
        list2.stream().forEachOrdered(System.out::println);

        /**
         * toArray 转成数组
         */
        Object[] array3 = list.stream().toArray();
        Object[] array4 = list.toArray();
        System.out.println(array3);
        System.out.println(array4);

        /**
         * reduce 归约   前面的a+后面的b+后面的b+...
         */
        List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        
        String reduce = list.stream().reduce((a, b) -> a + b.length()).get();
        String reduce2 = list.stream().reduce("reduce ", (a, b) -> a + b.length());
        Integer reduce3 = ints.stream().reduce(11, (a, b) -> a + b);
        System.out.println(reduce);
        System.out.println(reduce2);
        System.out.println(reduce3);

        /**
         * collect  
         */
        List<String> collect9 = list.stream().collect(Collectors.toList());
        System.out.println(collect9);

        /**
         * min 最小的 参数是Comparator
         */
        String min = list.stream().min((h1, h2) -> h1.compareTo(h2)).get();
        System.out.println(min);

        /**
         * max 最大的 参数是Comparator
         */
        String max = list.stream().max((h1, h2) -> h1.compareTo(h2)).get();
        System.out.println(max);

        /**
         * count 元素的总数
         */
        long count = list.stream().count();
        System.out.println(count);

        /**
         * anyMatch 任何一个 返回值boolean
         */
        boolean anyMatch = list.stream().anyMatch(s -> s.length() > 7);
        System.out.println(anyMatch);

        /**
         * allMatch 所有的 返回值boolean
         */
        boolean allMatch = list.stream().allMatch(s -> s.length() > 1);
        System.out.println(allMatch);

        /**
         * noneMatch 没有一个 返回值boolean
         */
        boolean noneMatch = list.stream().noneMatch(s -> s.length() > 7);
        System.out.println(noneMatch);

        /**
         * findFirst 找到第一个 返回值Optional<T>
         */
        String string = list.stream().filter(s->s.length()>2).findFirst().get();
        System.out.println(string);

        /**
         * findAny 
         */
        String string2 = list.stream().filter(s -> s.length() > 3).peek(s -> System.out.println(s)).findAny().get();
        System.out.println(string2);

        /**
         * 创建stream方法  Stream的静态方法
         * 
         * builder static方法  返回值Builder
         */
        java.util.stream.Stream.Builder<String> builder1 = java.util.stream.Stream.builder();
        builder1.accept("ss");
        builder1.add("xxx");
        Builder<String> add = builder1.add("xxx1");
        java.util.stream.Stream<String> stream1 = add.build();
        java.util.stream.Stream<String> stream = builder1.build();
        List<String> collect10 = stream.collect(Collectors.toList());
        List<String> collect11 = stream1.collect(Collectors.toList());
        System.out.println(collect10);
        System.out.println(collect11);
        
        /**
         * empty  创建一个不含任何元素的Stream
         */
        java.util.stream.Stream<Object> empty = java.util.stream.Stream.empty();
        List<Object> collect16 = empty.collect(Collectors.toList());
        System.out.println(collect16);
        
        /**
         * of 转换成String的静态发放 ,里面可以是 数组,list,
         */
        Map<String,String> map = new HashMap<String,String>();
        map.put("1", "a");
        map.put("2", "b");
        map.put("3", "c");
        List<String> collect13 = java.util.stream.Stream.of("ssss,sss,ss,1111".split(",")).collect(Collectors.toList());
        List<List<String>> collect14 = java.util.stream.Stream.of(list,list2,list1).collect(Collectors.toList());
        List<Map<String,String>> collect15 = java.util.stream.Stream.of(map,map,map).collect(Collectors.toList());
        System.out.println(collect13);
        System.out.println(collect14);
        System.out.println(collect15);
        
        /**
         * iterate 循环1到n 必须要有limit限制    和reduce类似
         */
        java.util.stream.Stream.iterate(0 , n -> n +3).limit(10).forEach(System.out::println);
        
        /**
         * generate  创建无限Stream的静态方法
         */
        java.util.stream.Stream<Double> echos = java.util.stream.Stream.generate(Math::random);
        System.out.println(echos.findFirst().get()+"======");
        Random seed = new Random();
        Supplier<Integer> random = seed::nextInt;
        java.util.stream.Stream.generate(random).limit(10).forEach(System.out::println);
    
        /**
         * concat   static方法   拼接stream  返回值stream
         */
        java.util.stream.Stream<String> concat = java.util.stream.Stream.concat(list.stream(), list1.stream());
        List<String> collect12 = concat.collect(Collectors.toList());
        System.out.println(collect12);
        
        /**
         * parallel  并行流
         */
        list.stream().parallel().forEach(System.out::println);
        list.stream().forEach(System.out::println);
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值