创建Stream方式1:通过集合List(常用)
创建Stream方式2:通过数组(常用)
创建Stream方式3:通过Stream的of()来创建
创建Stream方式4:创建无限流
详情见代码
public class StreamApiTest {
/**
* 创建Stream方式1:通过集合List
*/
@Test
public void test1() {
List<Employee> emp = EmployeeData.getEmp();
//通过集合来创建
//顺序流
Stream<Employee> stream = emp.stream();
//并行流
Stream<Employee> employeeStream = emp.parallelStream();
}
/**
* 创建Stream方式2:通过数组
*/
@Test
public void test2() {
int arr[] = new int[]{1, 2, 3, 4, 5, 6};
//调用Arrays.stream返回数组
IntStream stream = Arrays.stream(arr);
Employee e1 = new Employee(1, "1", 1, 1);
Employee e2 = new Employee(2, "1", 1, 1);
Employee employees[] = new Employee[]{e1, e2};
Stream<Employee> stream1 = Arrays.stream(employees);
}
/**
* 创建Stream方式3: 通过Stream的of()来创建
*/
@Test
public void test3() {
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
Stream<? extends Number> stream = Stream.of(1.0, 2, 3, 4, 5, 6);
}
/**
* 创建Stream方式4:创建无限流
*/
@Test
public void test4() {
//迭代
//遍历前10个偶数
Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
//生成
Stream.generate(Math::random).limit(10).forEach(System.out::println);
}
附上Employee.java和EmployeeData.java
Employee.java
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(int id) {
this.id = id;
}
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
if (getId() != employee.getId()) return false;
if (getAge() != employee.getAge()) return false;
if (Double.compare(employee.getSalary(), getSalary()) != 0) return false;
return getName() != null ? getName().equals(employee.getName()) : employee.getName() == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = getId();
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + getAge();
temp = Double.doubleToLongBits(getSalary());
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Employee() {
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
EmployeeData.java
public class EmployeeData {
public static List<Employee> getEmp() {
List<Employee> list = new ArrayList<>();
list.add(new Employee(1001, "马化腾", 34, 6000.38));
list.add(new Employee(1002, "马云", 12, 9876.12));
list.add(new Employee(1003, "刘强东", 33, 3000.82));
list.add(new Employee(1004, "雷军", 26, 7657.37));
list.add(new Employee(1005, "李彦宏", 65, 5555.21));
list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
list.add(new Employee(1006, "任正非", 26, 4333.32));
list.add(new Employee(1006, "扎克伯格", 35, 2500.32));
return list;
}