Java8 Stream的筛选和切片

写多了Spark/Scala,这个是比较简单的。

package com.expgiga.Java8;

/**  *  */ public class Employee {
    private String name;
    private int age;
    private double salary;
    private int id;

    public Employee() {
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    public Employee(int id) {
        this.id = id;
    }

    public Employee(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (age != employee.age) return false;
        if (Double.compare(employee.salary, salary) != 0) return false;
        if (id != employee.id) return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        temp = Double.doubleToLongBits(salary);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + id;
        return result;
    }
}

package com.expgiga.Java8;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

/**  * Java8 Stream  *  * 一、Stream的三个操作步骤:  * 1. 创建Stream  * 2. 中间操作  * 3. 终止操作(终端操作)  */  public class TestStream2 {
    public static void main(String[] args) {
        /*  * 中间操作:  * filter:接受Lambda,从流中排出某些元素。  * limit:截断流,使其元素不超过给定数量  * skip:跳过元素,返回一个扔掉了前n个元素的流。若流中元素不足n个,则返回一个空流。与Limit(n)互补  * distinct:筛选,通过流所生成元素的hashCode()equals()去除重复元素  */  //内部迭代:迭代操作由Stream API完成  List<Employee> employeeList = Arrays.asList(
                new Employee("zhangsan", 18, 19999),
                new Employee("lisi", 28, 29999),
                new Employee("wangwu", 38, 39999),
                new Employee("zhaoliu", 16, 17999),
                new Employee("tianqi", 6, 12999),
                new Employee("tianqi", 6, 12999),
                new Employee("tianqi", 6, 12999)
        );
        Stream<Employee> stream = employeeList.stream()
                .filter((e) -> {
                    System.out.println("Stream API的中间操作");
                    return e.getAge() > 18;
                }); //中间操作不会有任何结果   //终止操作:一次性执行全部内容,即"惰性求值"  stream.forEach(System.out::println);

        //外部迭代:  Iterator<Employee> it = employeeList.iterator();

        while (it.hasNext()) {
            System.out.println(it.next());
        }

        employeeList.stream()
                .filter((e) -> {
                    System.out.println("短路!"); //&& ||  return e.getSalary() > 5000;
                })
                .limit(3)
                .forEach(System.out::println);

        employeeList.stream()
                .filter((e) -> e.getSalary() > 5000)
                .skip(2)
                .forEach(System.out::println);

        employeeList.stream()
                .filter((e) -> e.getSalary() > 5000)
                .skip(2)
                .distinct()
                .forEach(System.out::println);//Employee必须重写hashCode()equals()   }
}

    原文作者:u013063153
    原文地址: https://blog.csdn.net/u013063153/article/details/76736928
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞