Java8实战阅读笔记(流操作)

4.4 流操作

  List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
        // 去重 筛选流
        numbers.stream().filter(i ->i % 2 == 0)
                .distinct();

        // 截短流
        List<Integer> collect = numbers.stream()
                .filter((i) -> i == 5)
                .limit(3)
                .collect(Collectors.toList());
        // 映射
        List<String> words = Arrays.asList("java8","lambdas","in","action");
        List<Integer> wordLengths = words.stream()
                .map((s)->s.substring(0,1))
                .map(String::length)
                .collect(Collectors.toList());
        wordLengths.stream().forEach(System.out::print);

        // 流的扁平化   返回的为数组类型
        List<String[]> collect1 =
                words.stream()
                        .map(w -> w.split(""))
                        .distinct()
                        .collect(Collectors.toList());

        // flatMap把一个流中的每个值都换成另一个流
        words.stream().map(w->w.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());

        // 查找匹配 检查谓词是否至少匹配一个元素  单个
        if(words.stream().anyMatch(s->s.length() >5)){
                // 检查words里面是否有长度大于5的单词
        }
        // 多个匹配 检查里面的单词长度是否全部大于4
        if(words.stream().allMatch(s->s.length() > 4)){

        }
        // 检查里面的单词长度是否没有大于5的
        if(words.stream().noneMatch(s->s.length() > 5)){

        }

        // 查找元素 查找单个元素
        Optional<String> any = words.stream()
                .filter(s -> s.length() > 5).findAny();

        // 查找第一个元素
        Optional<Integer> first = words.stream().map(String::length).findFirst();

 	 // 要是我还要乘 则需要复制上面的代码
        // 0为初始值
        Integer reduce = collect2.stream().reduce(0, (a, b) -> a + b);
        Integer reduce1 = collect2.stream().reduce(0, Integer::sum);
        Optional<Integer> reduce2 = collect2.stream().reduce(Integer::max);
        Optional<Integer> reduce3 = collect2.stream().reduce(Integer::min);

		 // 得到收集器
        IntSummaryStatistics collect1 = apples.stream().collect(summarizingInt(Apple::getWeight));
        // 收集器里包含了最大值 最小值 平均值.....
        double average = collect1.getAverage();
        int max = collect1.getMax();



 		// 连接字符串
        // 所有苹果颜色拼接成字符串

   String d = apples.stream().map(Apple::getColor).collect(joining(","));
        // 计算苹果的总重量
        Integer sumApple = apples.stream().map(Apple::getWeight).reduce(0, (a, b) -> a + b);
        apples.stream().collect(reducing(0,Apple::getWeight,Integer::sum));
        apples.stream().map(Apple::getWeight).reduce(Integer::sum).get();
        apples.stream().mapToInt(Apple::getWeight).sum();

	   // 根据颜色数量进行分组 key为颜色  value为数量
        Map<String, Long> collect4 = apples.stream().collect(groupingBy(Apple::getColor,counting()));

        // 每组不同颜色的苹果 最重得重量
        Map<String, Optional<Apple>> collect5 = apples.stream().collect(groupingBy(Apple::getColor,maxBy(Comparator.comparingInt(Apple::getWeight))));

        // 去掉Optional
        Map<String, Apple> collect6 = apples.stream()
                .collect(groupingBy(Apple::getColor, collectingAndThen(maxBy(Comparator.comparingInt(Apple::getWeight)), Optional::get)));
        // 条件分组筛选
        Map<String, HashSet<Boolean>> collect7 = apples.stream().collect(groupingBy(Apple::getColor, mapping(a -> {
            if (a.getWeight() > 100) {
                return true;

            }
            return false;
        }, toCollection(HashSet::new))));

        // 分区
        Map<Boolean, List<Dish>> collect8 = menus.stream().collect(partitioningBy(Dish::isVegetarian));

        Map<Boolean, Map<Dish.Type, List<Dish>>> collect9 = menus.stream()
                .collect(partitioningBy(Dish::isVegetarian, groupingBy(Dish::getType)));

        Map<Boolean, Dish> collect10 = menus.stream()
                .collect(partitioningBy(Dish::isVegetarian, collectingAndThen(maxBy(Comparator.comparingInt(Dish::getCalories)), Optional::get)));

在这里插入图片描述

4.5 构建流

     	// 值构建
        Stream<String> java = Stream.of("java", "demo");
        // 数值创建
        int[] numbers = {1,2,3,45,5};
        IntStream stream = Arrays.stream(numbers);
        Stream<Integer> boxed1 = stream.boxed();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值