Stream的中间操作(下)

Stream的中间操作

查找与匹配

allMatch----检查是否匹配所有元素
anyMatch----检查是否至少匹配一个元素
noneMatch---检查是否没有匹配的元素
findFirst----返回第一个元素
findAny----返回当前流中的任意元素
count-----返回流中元素上的总个数
max----返回流中最大值
min----返回流中最小值

 List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.FREE),
            new Employee("李四", 48, 3333.99, Employee.Status.BUSY),
            new Employee("王五", 38, 5999.99, Employee.Status.VOCATION),
            new Employee("赵六", 38, 5999.99, Employee.Status.BUSY),
            new Employee("赵六", 8, 6999.99, Employee.Status.VOCATION)
    );

    /**
     * 查找与匹配
     * allMatch----检查是否匹配所有元素
     * anyMatch----检查是否至少匹配一个元素
     * noneMatch---检查是否没有匹配的元素
     * findFirst----返回第一个元素
     * findAny----返回当前流中的任意元素
     * count-----返回流中元素上的总个数
     * max----返回流中最大值
     * min----返回流中最小值
     */
    @Test
    public void test() {
        boolean b = employees.stream().allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b);
        boolean b1 = employees.stream().anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1);
        boolean b2 = employees.stream().noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);
        employees.stream().sorted((e1,e2)->e1.getSalary().compareTo(e2.getSalary())).forEach(System.out::println);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Optional<Employee> first =
                employees.stream().sorted((e1, e2) -> -Double.compare(e1.getSalary(),e2.getSalary())).findFirst();
        Employee employee = first.get();
        System.out.println(employee);
        System.out.println("+++++++++++++++++++++++串行流++++++++++++++++++++++++++++++++++++");
        Optional<Employee> any = employees.stream().filter(e -> e.getStatus().equals(Employee.Status.FREE)).findAny();
        Employee employee1 = any.get();
        System.out.println(employee1);
        System.out.println("+++++++++++++++++++++++并行流++++++++++++++++++++++++++++++++++++");
        Optional<Employee> any1 =
                employees.parallelStream().filter(e -> e.getStatus().equals(Employee.Status.FREE)).findAny();
        Employee employee2 = any1.get();
        System.out.println(employee2);
    }
    @Test
    public void test2(){
        long count = employees.stream().count();
        System.out.println(count);

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

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

        Optional<Double> min1 = employees.stream().map(Employee::getSalary).min(Double::compare);
        System.out.println(min1);
    }

归约

reduce(T identity,BinaryOperator)/reduce(BinaryOperator)----可以将流中元素反复结合起来,得到一个值
备注:
map和reduce的连接通常称为map-reduce模式,因google用它来进行网络搜索而出名

/**
     * 归约
     * reduce(T identity,BinaryOperator)/reduce(BinaryOperator)----可以将流中元素反复结合起来,得到一个值
     *
     * 备注:
     *  map和reduce的连接通常称为map-reduce模式,因google用它来进行网络搜索而出名
     */
    @Test
    public void test3(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream().reduce(0, (x, y) -> x + y);
        System.out.println(sum);

        System.out.println("******************************************************");
        Optional<Double> reduce = employees.stream().map(Employee::getSalary).reduce(Double::sum);
        System.out.println(reduce.get());
    }

收集

collect----将流转换为其他形式。接收一个Collector接口的实现,用于给Stream中元素做汇总的方法
     /**
     * 收集
     *  collect----将流转换为其他形式。接收一个Collector接口的实现,用于给Stream中元素做汇总的方法
     */
 	@Test
    public void test4(){
        List<String> collect = employees.stream().map(Employee::getName).collect(Collectors.toList());
        for (String str:collect) {
            System.out.println(str);
        }
        System.out.println("********************************************************");
        employees.stream().map(Employee::getName).collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("********************************************************");
        employees.stream().map(Employee::getName).collect(Collectors.toSet()).forEach(System.out::println);
        System.out.println("********************************************************");
        employees.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new)).forEach(System.out::println);
    }
    @Test
    public void test5(){
        //总数
        Long count = employees.stream().collect(Collectors.counting());
        System.out.println(count);
        System.out.println("********************************************************");
        //平均值
        Double collect1 = employees.stream().collect(Collectors.averagingDouble(Employee::getSalary));
        System.out.println(collect1);
        System.out.println("********************************************************");
        //总和
        Double collect2 = employees.stream().collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(collect2);
        System.out.println("********************************************************");
        //最大值
        Optional<Employee> collect3 =
                employees.stream().collect(Collectors.maxBy((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())));
        System.out.println(collect3.get().getSalary());
        Optional<Employee> collect31 =
                employees.stream().collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(),e2.getSalary())));
        System.out.println(collect31.get().getSalary());
        System.out.println("********************************************************");
        //最小值
        Optional<Employee> collect4 =
                employees.stream().collect(Collectors.minBy((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())));
        System.out.println(collect4.get().getSalary());
        Optional<Employee> collect41 =
                employees.stream().collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary(),e2.getSalary())));
        System.out.println(collect41.get().getSalary());

        Optional<Double> collect42 =
                employees.stream().map(Employee::getSalary).collect(Collectors.minBy(Double::compare));
        System.out.println(collect42.get());
    }
    //分组
    @Test
    public void test6(){
        Map<Employee.Status, List<Employee>> collect =
                employees.stream().collect(Collectors.groupingBy(Employee::getStatus));
        System.out.println(collect);
    }
    //多级分组
    @Test
    public void test7(){
        Map<Employee.Status, Map<String, List<Employee>>> collect =
                employees.stream().collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
            if (e.getAge() <= 35) {
                return "青年";
            } else if (e.getAge() <= 50) {
                return "中年";
            } else {
                return "老年";
            }
        })));
        System.out.println(collect);
    }
    //分区
    @Test
    public void test8(){
        Map<Boolean, List<Employee>> collect =
                employees.stream().collect(Collectors.partitioningBy((e) -> e.getSalary() > 8000));
        System.out.println(collect);
    }

    @Test
    public void test9(){
        DoubleSummaryStatistics collect = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println(collect.getAverage());
        System.out.println(collect.getMax());
        System.out.println(collect.getMin());
        System.out.println(collect.getCount());
    }

    @Test
    public void test10(){
        String str = employees.stream().map(Employee::getName).collect(Collectors.joining(",","----","----"));
        System.out.println(str);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值