StreamAPI的使用学习与总结

Stream API
Stream操作的三个步骤
创建stream
中间操作(过滤、map)
终止操作
stream的创建:
// 1,校验通过Collection 系列集合提供的stream()或者paralleStream()
List list = new ArrayList<>();
Strean stream1 = list.stream();

// 2.通过Arrays的静态方法stream()获取数组流
String[] str = new String[10];
Stream<String> stream2 = Arrays.stream(str);

// 3.通过Stream类中的静态方法of
Stream<String> stream3 = Stream.of("aa","bb","cc");

// 4.创建无限流
// 迭代
Stream<Integer> stream4 = Stream.iterate(0,(x) -> x+2);

//生成
Stream.generate(() ->Math.random());

Stream的中间操作:
/**

  • 筛选 过滤 去重
    */
    emps.stream()
    .filter(e -> e.getAge() > 10)
    .limit(4)
    .skip(4)
    // 需要流中的元素重写hashCode和equals方法
    .distinct()
    .forEach(System.out::println);

/**

  • 生成新的流 通过map映射
    */
    emps.stream()
    .map((e) -> e.getAge())
    .forEach(System.out::println);

/**

  • 自然排序 定制排序
    */
    emps.stream()
    .sorted((e1 ,e2) -> {
    if (e1.getAge().equals(e2.getAge())){
    return e1.getName().compareTo(e2.getName());
    } else{
    return e1.getAge().compareTo(e2.getAge());
    }
    })
    .forEach(System.out::println);

Stream的终止操作:
/**
* 查找和匹配
* allMatch-检查是否匹配所有元素
* anyMatch-检查是否至少匹配一个元素
* noneMatch-检查是否没有匹配所有元素
* findFirst-返回第一个元素
* findAny-返回当前流中的任意元素
* count-返回流中元素的总个数
* max-返回流中最大值
* min-返回流中最小值
*/

    /**
     *  检查是否匹配元素
     */
    boolean b1 = emps.stream()
            .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
    System.out.println(b1);

    boolean b2 = emps.stream()
            .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
    System.out.println(b2);

    boolean b3 = emps.stream()
            .noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
    System.out.println(b3);

    Optional<Employee> opt = emps.stream()
            .findFirst();
    System.out.println(opt.get());

    // 并行流
    Optional<Employee> opt2 = emps.parallelStream()
            .findAny();
    System.out.println(opt2.get());

    long count = emps.stream()
            .count();
    System.out.println(count);

    Optional<Employee> max = emps.stream()
            .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
    System.out.println(max.get());

    Optional<Employee> min = emps.stream()
            .min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
    System.out.println(min.get());

还有功能比较强大的两个终止操作 reduce和collect
reduce操作: reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以将流中元素反复结合起来,得到一个值
/**
* reduce :规约操作
*/
List list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer count2 = list.stream()
.reduce(0, (x, y) -> x + y);
System.out.println(count2);

    Optional<Double> sum = emps.stream()
            .map(Employee::getSalary)
            .reduce(Double::sum);
    System.out.println(sum);

collect操作:Collect-将流转换为其他形式,接收一个Collection接口的实现,用于给Stream中元素做汇总的方法
/**
* collect:收集操作
*/

    List<Integer> ageList = emps.stream()
            .map(Employee::getAge)
            .collect(Collectors.toList());
    ageList.stream().forEach(System.out::println);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值