java8- minimization (Method 8)

test:
List <the Employee> = the Employees Arrays.asList (
         new new the Employee (101, "Joe Smith", 18 is, 9999.99 ),
         new new the Employee (102, "John Doe", 59, 6666.66 ),
         new new the Employee (103, "Wang Wu" 28, 3333.33 ),
         new new the Employee (104, "Zhao six". 8, 7777.77 ),
         new new the Employee (105, "pseudo-ginseng", 38, 5555.55 ) 
); 
 
    / ** 
     * Note: the parameter list 
     * 1. implemented interface the method parameter list and return value to the 
     parameter references an instance method * method that returns a list of values consistent; 
     * 2.lamdba expression not going to achieve its own method, the method of its use -> instead of 
     * only the relationship between the parameter list and the method body 
     * 3.The method or methods belong to the class variables or instance method 
     * / 
    @Test 
    public  void test1 () {
        Consumer <String> Consumer = (X) -> System.out.println (X); // create an instance of 
        Consumer <String> consumer1 the println :: = the System.out;    // Create instance 
    } 
    @Test 
    public  void Test3 () { 
        Comparator <Integer> = Comparator (a1, a2) -> Integer.compare (a1, a2);
         / ** 
         * corresponds to a1 and a2 are transmitted to compare 
         * / 
        Comparator <Integer> = comparator1 Integer :: compare; 
    } 
    // the reduce ----> the stream of elements repeatedly combined to give a value 
    / ** 
     * the reduce T (T Identity, BinaryOperator <T> ACC with); 
     * <P>
     Optional The * <T> the reduce (BinaryOperator <T> ACC with); 
     * / 
    @Test 
    public  void Test4 () { 
        List <Integer> Arrays.asList List = (. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8 ) ;
         // this fact is emphasized that the BizFunction Apply 
        Integer = list.stream the reduce () the reduce (0, (x, y) -> + x. y);
         // x and y as the sum of the parameters passed to 
        Integer = list.stream reduce1 () the reduce (0. , Integer :: SUM);
         // find the maximum value 
        Integer Result = list.stream () 
                .reduce (BinaryOperator 
                        .maxBy (Integer :: Compare)) orElseGet ((). -> {
                    return 0 ; 
                }); 
        System.out.println (the reduce); 
        System.out.println (reduce1); 
        System.out.println (Result); 
    } 
    // company sum of wages
     // lamdba expression method can be used instead of reference, the two are inseparable
     // pass and do not pass the reference parameters
     // Map and the reduce 
    @Test
     public  void Test5 () { 
        Double adouble = employees.stream () 
                .map (E -> e.getSalary ( .)) the reduce ((X, Y) -> X + Y) 
                .get (); 
//         ; String.format ( "% D", adouble) 
        Double aDouble1 =employees.stream () 
                .map (the Employee :: getSalary) 
                .reduce (Double :: SUM) 
                .get (); 
        // find maximum 
        Double aDouble2 = employees.stream () 
                .map (the Employee :: getSalary) 
                .reduce ( BinaryOperator 
                        .maxBy (Double :: the compareTo)) GET ();. 
        System.out.println (adouble); 
        System.out.println (aDouble1); 
        System.out.println (aDouble2); 
    } 

    // Supplier-- supply, It is supplied not only data, but also any container supplied 
    @Test
     public  void Test () { 
        HashSet <the Employee> Result = employees.stream ()
                .collect (Collectors.toCollection (HashSet :: new new )); 
        System.out.println (Result); 
    } 
    // Counting 
    @Test
     public  void TEST7 () { 
        Long the collect = employees.stream () 
                .collect (Collectors.counting ( )); 
        System.out.println (the collect); 
    } 
    // averaging 
    / ** 
     * there are methods where there is a reference lamdba expression, because it can be understood that the outer 
     * / 
    @Test 
    public  void test8 () { 
        Double the collect = employees.stream () 
                .collect (Collectors.averagingDouble (the 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());
    }

 

----------------
Disclaimer: This article is CSDN bloggers' sailing to transform IT, the dream always on the road "in the original article, follow the CC 4.0 BY-SA copyright agreement, reprint Please include links to the original source and this statement.
Original link: https: //blog.csdn.net/wb_zjp283121/article/details/90782267

Guess you like

Origin www.cnblogs.com/moonsoft/p/12514835.html