java8-求最小值(8中方法)

List<Employee> employees = Arrays.asList(
        new Employee(101, "张三", 18, 9999.99),
        new Employee(102, "李四", 59, 6666.66),
        new Employee(103, "王五", 28, 3333.33),
        new Employee(104, "赵六", 8, 7777.77),
        new Employee(105, "田七", 38, 5555.55)
);

 

    /**
     * 注意:参数列表
     * 1.实现的接口中的方法的参数列表和返回值要与
     * 方法引用中的实例方法的参数列表这返回值一致;
     * 2.lamdba表达式不会去实现它本身的方法,这个方法其使用->代替
     * 只关系参数列表和方法体
     * 3.方法或者变量属于类方法或者实例方法
     */

    @Test
    public void test1() {
        Consumer<String> consumer = (x) -> System.out.println(x); //创建实例
        Consumer<String> consumer1 = System.out::println;   //创建实例
    }

    @Test
    public void test3() {
        Comparator<Integer> comparator = (a1, a2) -> Integer.compare(a1, a2);
        /**
         *相当于把a1和a2传到compare中去
         */
        Comparator<Integer> comparator1 = Integer::compare;
    }

    //reduce   ---->将流中的元素反复结合起来,得到一个值

    /**
     * T reduce(T identity, BinaryOperator<T> accumulator);
     * <p>
     * Optional<T> reduce(BinaryOperator<T> accumulator);
     */
    @Test
    public void test4() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        //这个其实调的是BizFunction中的apply
        Integer reduce = list.stream().reduce(0, (x, y) -> x + y);
        //将x和y作为参数传到sum中
        Integer reduce1 = list.stream().reduce(0, Integer::sum);
        //求其最大值
        Integer result = list.stream()
                .reduce(BinaryOperator
                        .maxBy(Integer::compare)).orElseGet(() -> {
                    return 0;
                });
        System.out.println(reduce);
        System.out.println(reduce1);
        System.out.println(result);
    }

    //公司中员工工资的总和
    //lamdba表达式可以用方法引用代替,这个二者是分不开的
    //传参和不传参
    //map和reduce
    @Test
    public void test5() {

        Double aDouble = employees.stream()
                .map(e -> e.getSalary()).reduce((x, y) -> x + y)
                .get();

//        String.format("d%", aDouble);
        Double aDouble1 = employees.stream()
                .map(Employee::getSalary)
                .reduce(Double::sum)
                .get();

        //求最大值
        Double aDouble2 = employees.stream()
                .map(Employee::getSalary)
                .reduce(BinaryOperator
                        .maxBy(Double::compareTo)).get();

        System.out.println(aDouble);
        System.out.println(aDouble1);
        System.out.println(aDouble2);
    }


    //Supplier--供给,不仅供给数据,任何容器也是供给
    @Test
    public void test() {
        HashSet<Employee> result = employees.stream()
                .collect(Collectors.toCollection(HashSet::new));
        System.out.println(result);
    }

    //counting
    @Test
    public void test7() {
        Long collect = employees.stream()
                .collect(Collectors.counting());
        System.out.println(collect);
    }

    //求平均值

    /**
     * 有lamdba表达式的地方就有方法引用,因为这样才能理解到外
     */
    @Test
    public void test8() {
        Double collect = employees.stream()
                .collect(Collectors.averagingDouble(Employee::getSalary));
        Double collect1 = employees.stream().collect(Collectors.averagingDouble(o -> o.getSalary()));
        System.out.println(collect);
        System.out.println(collect1);
    }

    /**
     * sum
     */
    @Test
    public void test9() {
        Double sumResult = employees.stream().collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(sumResult);
    }

    /**
     * java8求最小值的几种写法
     */
    @Test
    public void test10() {

        //1.
        Optional<Employee> result = employees.stream().collect(Collectors.minBy(((o1, o2) -> (int) (o1.getSalary() - o2.getSalary()))));
        Employee employee = result.get();
        System.out.println(employee.getSalary());

        //2.
        Employee employee2 = employees.stream()
                .collect(Collectors
                        .minBy(Comparator.
                                comparingDouble((employee1) -> employee1.getSalary()))).get();
        System.out.println(employee2.getSalary());

        //3.
        Employee employee3 = employees.stream().min(Comparator.
                comparingDouble((employee1) -> employee1.getSalary())).get();


        //4.
        Employee employee4 = employees.stream()
                .collect(Collectors.minBy(Comparator.comparing(Employee::getSalary))).get();

        //5.也可以根据map+reduce的方法进行

        System.out.println(employee.getSalary());
        System.out.println(employee2.getSalary());
        System.out.println(employee3.getSalary());
        System.out.println(employee4.getSalary());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值