stream - Screening and slice (b)

* Filter- receiving Lambda, exclude certain elements from the stream.
* 1imit- cut off the flow, so that the element does not exceed a given number.
* Skip (n) - skip element, a return flow throw the first n elements. If the flow is less than n number of elements, an empty stream, and 1imit (n) is complementary.
* Distinct- screening, hashCode elements generated by the flow () and equals () removing repetitive elements.

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

1, external iteration

    @Test
    public void test0() {
        Iterator<Employee> iterator = employees.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

2, the internal filter iteration: performed by steam api

    public  void test1 () {
         // intermediate operating 
        Stream <the Employee> = employees.stream filter () filter ((E) -.> {
            System.out.println ( "API intermediate operating Stream" );
             return e.getAge ()> 35 ;
        });
        // termination operation, the entire content is performed once: lazy evaluation 
        filter.forEach (System.out :: println);
    }

2 before 3, limit the condition of taking

    @Test
    public void test2() {
        employees.stream().filter((e) -> {
            System.out.println ( "Once satisfied, no further follow-up operation" );
             return e.getSalary ()> 50 ;
        }).limit(2).forEach(System.out::println);
    }

4, the first skip skip 2

    @Test
    public void test3() {
        employees.stream().filter((e) -> e.getSalary() > 50).skip(2).forEach(System.out::println);
    }

5, distinct deduplication, for an override hashCode and equals

    @Test
    public void test4() {
        employees.stream().filter((e) -> e.getSalary() > 50).distinct().forEach(System.out::println);
    }

 

Guess you like

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