java8 list统计(Stream API )

list.stream().mapToDouble(User::getHeight).sum()//和
list.stream().mapToDouble(User::getHeight).max()//最大
list.stream().mapToDouble(User::getHeight).min()//最小
list.stream().mapToDouble(User::getHeight).average()//平均值

有关java Stream 流式处理得详细列子:

package com.wyz.streamApisToMap;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.IntSummaryStatistics;

import java.util.Map;
public class StreamAPi {
    
    public static void main(String[] args){
        // 初始化
        List<Integer> integerList = Arrays.asList(1,2,3,4,5,6,7,6,5,4,3,2,1);
        int sumR = 0;
        for(int i =0;i<integerList.size();i++){
            sumR += integerList.get(i);
        }
        
        System.out.println("普通求和:"+sumR);
        //过滤,收集所有偶数
        List<Integer> collect = integerList.stream()
                .filter(Integer-> Integer%2 == 0)
                .collect(Collectors.toList());
        
        System.out.println("过滤,收集所有偶数:"+collect);
        
        int sum = integerList.stream().mapToInt(Integer::intValue).sum();
        System.out.println("计算总和:"+sum);
        
        // 计算求和
        int reduce = integerList.stream()
                .mapToInt(Integer::intValue)
                .reduce(0, Integer::sum);
        System.out.println("计算总和,带初始值:"+reduce);
        
        OptionalInt reduce1 =integerList.stream()
                .mapToInt(Integer::intValue)
                .reduce(Integer::sum);
        
        System.out.println("计算总和,OptiionalInt :"+reduce1);
        
        int reduce2 = integerList.stream()
                .reduce(0, (a,b)->a+b);
        System.out.println("计算总和:"+reduce2);
        
        //计算数量
        long count = integerList.stream().count();
        System.out.println("计算数量"+count);
        
        Optional<Integer> collect8 = integerList.stream().collect(Collectors.maxBy((x1,x2)->x1-x2));
        Optional<Integer> collect9 = integerList.stream().collect(
                Collectors.maxBy(Comparator.comparing(Integer::intValue)));
        if(collect8.isPresent()){
            System.out.println("求最大值:"+collect8.get());
        }
        
        if(collect9.isPresent()){
            System.out.println("求最大值:"+collect9.get());
        }
        
        Optional<Integer> collect10 = integerList.stream().collect(Collectors.
                minBy(Comparator.comparing(Integer::intValue)));
        if(collect10.isPresent()){
            System.out.println("求最小值:"+collect10.get());
        }
        
        // 求平均值
        Double collect11 = integerList.stream().collect(Collectors.averagingInt(Integer::intValue));
        System.out.println("平均值:"+collect11);
        
        // 一次性得到元素个数、总和、均值、最大值、最小值
        IntSummaryStatistics collect12 = integerList.stream().collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println("一次性得到元素个数、总和、均值、最大值、最小值:"+collect12);
        
        //分组
        Map<Integer, List<Integer>> collect15 = integerList.stream().collect(
                Collectors.groupingBy(Integer::intValue)

        );
         System.out.println("分组:"+collect15);
         
         Map<Integer, Long> collect14 = integerList.stream().collect(
                 Collectors.groupingBy(Integer::intValue, Collectors.counting())
         );
         System.out.println("可以有多级分组:"+collect14);
         
         //分区可以看做是分组的一种特殊情况,在分区中key只有两种情况:true 或false
         Map<Boolean,List<Integer>> collect16 = integerList.stream().collect(Collectors.partitioningBy(x->x >= 7));
         System.out.println("分区可以看做是分组的一种特殊情况,在分区中key 只有两种情况:true或false:"+collect16);
         // 去重
         List<Integer> collect1 = integerList
                 .stream()
                 .distinct()
                 .collect(Collectors.toList());
         System.out.println("去重:"+collect1);
         
         //limit 返回包含前n个元素的流
         List<Integer> collect2 = integerList
                 .stream()
                 .filter(Integer-> Integer %2==0)
                 .limit(2).collect(Collectors.toList());
         System.out.println("limit:"+collect2);
         //排序 倒叙排序
         List<Integer> collect3 = integerList.stream()
                 .sorted((s1,s2)->s2-s1)
                 .collect(Collectors.toList());
         System.out.println("排序:"+collect3);
         
         // 跳过前n个元素
         List<Integer> collect4 = integerList.stream()
                 .filter(Integer -> Integer %2==1).skip(3)
                 .collect(Collectors.toList());
         System.out.println("skip:"+collect4);
         
         String[] strs = {"java8","is","easy","to","use"};
         
         //字符串拼接
         String collect13 = Arrays.stream(strs).collect(Collectors.joining());
         System.out.println("字符串拼接:"+collect13);
          
         List<String> collect5 = Arrays.stream(strs)
                 .map(s->s.split(""))// 每个字符串映射成string
                 .flatMap(Arrays::stream)
                 .collect(Collectors.toList());
         
         System.out.println("将字符串映射成字符数组:"+collect5);
         //FlatMap 是将一个流中的每个值都转换成一个个流,然后再将这些流扁平化成为一个流
         List<String> collect7 = Arrays.stream(strs)
                 .map(s->s.split("")) // 每个字符串映射成string[]
                 .flatMap(Arrays::stream) // flatMap 将由Map 映射得到的Stream<String[]>,转换成由各个字符串数组映射成的流Stream<String>
                 .collect(Collectors.toList());
         System.out.println("flatMap是将一个流中的每个值都转成一个个流,然后再将这些流扁平化成为一个流:" + collect7);
         
         List<String> collect6 = Arrays.stream(strs)
                 .map(s->s.split(""))
                 .flatMap(Arrays::stream)
                 .distinct()
                 .collect(Collectors.toList());
          System.out.println("多个字符串,将各个字符串拆开去重:"+collect6);
          
          // allMatch 检测是否全部满足指定的参数行为
          boolean b = integerList.stream().allMatch(Integer->Integer>5);
          System.out.println("allMatch,检测是否全部都满足指定的参数行为:"+b);
          // anyMatch 检测是否存在一个或者多个满足指定的参数行为
          boolean any = integerList.stream().anyMatch(Integer->Integer >5);
          System.out.println("anyMatch,检测是否存在一个或多个满足指定的参数行为:"+any);
          // nonMatch 检测是否不存在满足指定行为的元素
          boolean non = integerList.stream().noneMatch(Integer ->Integer >5);
          System.out.println("检测是否不存在满足指定行为的元素:"+non);
          
          //返回满足条件的第一个元素
          Optional<Integer> first = integerList.stream().filter(Integer->Integer >6).findFirst();
          if(first.isPresent()){
              System.out.println("用于返回满足条件的第一个元素:"+first.get());
          }
          
          // findAny 相对于findFirs 区别在于 findAny 不一定返回第一个而是返回任意一个
          // 实际上对于顺序流式处理而言,findFirst和findAny 返回的结果是一样的,
          // 至于为什么会这样设计,当我们启用并行流式处理的时候,查找第一个元素往往会有很多限制,如果不式特别需求,
          // 在并行流式处理中使用findAny的性能要比findFirst 好
          
          Optional<Integer> any1 = integerList.stream().filter(Integer->Integer >1).distinct().findAny();
          if(any1.isPresent()){
              System.out.println("findAny 不一定返回第一个,而是返回任意一个:"+any1.get());
          }
          
          
          //? 启动并行流式处理虽然简单,只需要将Sream()替换成parallelStream()即可,
          // 但既然是并行,就会设计到多现场安全问题,所以在启用之前要确认并行是否值得
          //(并行得效率不一定高于顺序执行),另外就要保证线程安全。此两项无法保证,
          // 那么并行毫无意义,毕竟结果比速度更加重
          Optional<Integer> any2 = integerList.parallelStream().filter(Integer -> Integer >1)
                  .distinct().findAny();
          if(any2.isPresent()){
              System.out.println("并行流式处理:"+any2.get());
          }
    }

}
3.有关对象的数据过滤

//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
//输出查找结果
StudentInfo.printStudents(boys);

转载于:https://my.oschina.net/kuchawyz/blog/3056435

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值