1、stream创建
// 1、可以通过collection系列集合 stream(串行)、parallelStream(并行) List<String> list = new ArrayList<String>(); Stream<String> stream1 = list.stream(); // 2、通过Arrays中的静态stream方式创建 Employee[] employee = new Employee[10]; Stream<Employee> stream2 = Arrays.stream(employee); // 3、通过Stream中的静态方法of创建 Stream<String> stream3 = Stream.of("aa", "bb", "cc", "dd"); // 4、迭代 Stream<Integer> iterate = Stream.iterate(10, (x) -> x + 2); iterate.limit(10).forEach(System.out::println); // 5、生成 Stream.generate(() -> Math.random()).limit(10).forEach(System.out::println);
2、创建实体类
package com.zh.stream; import java.util.Date; public class Employee { private Integer age; private String name; private double salary; private Status status; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Status getStatus() { return status; } public void setStatus(Status status) { this.status = status; } public Employee() { } public Employee(Integer age) { this.age = age; } public Employee(Integer age, String name) { this.age = age; this.name = name; } public Employee(Integer age, String name, double salary) { this.age = age; this.name = name; this.salary = salary; } public Employee(Integer id, Date createTime, Date updateTime, Integer age, String name, double salary, Status status) { this.age = age; this.name = name; this.salary = salary; this.status = status; } public Employee(Integer age, String name, double salary, Status status) { this.age = age; this.name = name; this.salary = salary; this.status = status; } @Override public String toString() { return "Employee [age=" + age + ", name=" + name + ", salary=" + salary + ", status=" + status + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(salary); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary)) return false; return true; } public enum Status { FREE, BUSY, VOCATION } }