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());
    }

 

————————————————
版權聲明:本文為CSDN博主「航海到IT的轉變,夢想一直在路上」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wb_zjp283121/article/details/90782267


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM