jdk1.8流处理Dmeo

     Stream<String> list = Stream.of("A","B","C","D");

        Optional<String> first = list.findFirst();
        System.out.println(first.get());

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        //map数据流的映射处理
        List<Integer> squares = numbers.stream().map(n -> n * n).collect(Collectors.toList());
        System.out.println(squares);

        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        //flatMap 处理多个流的逻辑最后转换为一个流
        List<int[]> pairs = numbers1.stream().flatMap(i -> numbers2.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());
        System.out.println(pairs);

        //条件查询匹配数据 anyMatch, allMatch,noneMatch,findFirst和findAny
        Predicate<Integer> predicate = a->a.equals(1);
        boolean b = numbers1.stream().anyMatch(predicate);//只要有元素匹配则返回true
        System.out.println(b);
        boolean a = numbers1.stream().allMatch(predicate);//所有元素都匹配则返回true
        System.out.println(a);
        boolean c = numbers1.stream().noneMatch(predicate);//所有元素都不匹配则返回true
        System.out.println(c);
        Integer d = numbers1.stream().findFirst().orElse(0);//获取第一个元素
        System.out.println(d);
        Integer e = numbers1.stream().findAny().orElse(0);//获取任意一个元素
        System.out.println(e);

        //汇总Collectors.summarizingInt, summarizingLong, summarizingDouble 数据统计
        IntSummaryStatistics collect = numbers1.stream().collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println(collect.getSum());
        System.out.println(collect.getAverage());
        System.out.println(collect.getMax());
        System.out.println(collect.getCount());

        //# Collectors.joining
        Stream<String> integerStream = Stream.of("1", "2", "3", "2", "4", "5");
        String s = integerStream.collect(Collectors.joining(","));
        System.out.println(s);

        //Collectors.groupingBy 分组
        Stream<String> groupingByStream = Stream.of("1", "2", "3", "2", "4", "5");
        Map<Boolean, List<String>> collect2 = groupingByStream.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0));
        System.out.println(collect2);
        //分组: groupingBy + mapping
        Stream<String> groupingByStream2 = Stream.of("1", "2", "3", "2", "4", "5");
        Map<Boolean, Set<String>> collect4 = groupingByStream2.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0,
                Collectors.mapping(xx -> xx+ "demo", Collectors.toSet())));
        System.out.println(collect4);
        //多级分组: groupingBy + groupingBy
        Stream<String> groupingByStream1 = Stream.of("1", "2", "3", "2", "4", "5");
        Map<Boolean, Map<Boolean, List<String>>> collect3 = groupingByStream1.collect(Collectors.groupingBy(as -> Integer.parseInt(as) % 2 == 0,
                Collectors.groupingBy(xx -> Integer.parseInt(xx) > 2)));
        System.out.println(collect3);



        Stream<String> stringStream = Stream.of("1", "2", "3", "2", "4", "5");
        ArrayList<Object> collect1 = stringStream.collect(ArrayList::new, List::add, List::addAll);
        System.out.println(JSON.toJSONString(collect1));

        //reduce 归约 T reduce(T identity, BinaryOperator<T> accumulator) identity初始化值
        Stream<String> stringStream1 = Stream.of("1", "2", "3", "2", "4", "5");
        String s1 = stringStream1.reduce("start", (x, y) -> x+","+y);
        System.out.println(s1);

 

posted @ 2021-08-23 17:43  A小小高  阅读(83)  评论(0编辑  收藏  举报