stream - Find and match (d)

Find matches

    List<Employee> employees = Arrays.asList(//
            new Employee(20, "张三", 5000.35, Status.FREE), //
            new Employee(40, "李四", 6500.63, Status.BUSY), //
            new Employee(30, "王五", 4000.93, Status.FREE), //
            new Employee(50, "赵六", 9005.36, Status.BUSY), //
            new Employee(10, "马七", 1050.93, Status.VOCATION), //
            new Employee(20, "朱八", 3000.73, Status.BUSY)//
    );

    @Test
    public void test1() {

        // . 1, allMatch- check match all elements 
        Boolean allMatch = employees.stream () allMatch ((E) ->. E.getStatus () the equals (Status.FREE).);
        System.out.println(allMatch);

        // 2, anyMatch- check matches at least one element 
        Boolean AnyMatch = employees.stream () AnyMatch ((E) ->. E.getStatus () the equals (Status.FREE).);
        System.out.println(anyMatch);

        // . 3, noneMatch- check that no match all elements 
        Boolean noneMatch = employees.stream () noneMatch ((E) ->. E.getStatus () the equals (Status.FREE).);
        System.out.println(noneMatch);

        // . 4, returns the first element findFirst- 
        Optional The <the Employee> the findFirst = employees.stream ()
                .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).findFirst();
        System.out.println(findFirst.get());

        // . 5, findAny- return any element in the current stream 
        Optional The <the Employee> findAny = employees.stream () filter ((E) ->. E.getStatus () the equals (Status.FREE).) FindAny ();.
        System.out.println(findAny.get());

        // . 6, Count - returns the total number of elements in the stream 
        Long COUNT = employees.stream () COUNT ().;
        System.out.println(count);

        // . 7, the maximum value of the return stream max- 
        Optional The <the Employee> employees.stream = max () max ((E1, E2) ->. Double.compare (e1.getSalary (), e2.getSalary ()));
        System.out.println(max.get());

        // . 8, a flow min returns a minimum value 
        min = Optional The <Double> employees.stream () Map (the Employee :: getSalary) .min (Double :: Compare).;
        System.out.println(min.get());
    }

 

Guess you like

Origin www.cnblogs.com/zhanh247/p/11854445.html