
package com.zs.boot.controller;
public class Employee {
private int id;
private String name;
private int age;
private double salary;
private Status status;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(int id, String name, int age, double salary, Status status) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
}
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
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 String show() {
return "测试方法引用!";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
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 != other.age)
return false;
if (id != other.id)
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", status=" + status
+ "]";
}
public enum Status {
FREE, BUSY, VOCATION;
}
}
package com.zs.boot.controller;
import org.junit.Test;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
public class TestStream2API {
List<Employee> employees = Arrays.asList(
new Employee("刘德华",48,8000),
new Employee("张学友",28,8500),
new Employee("黎明",45,6500),
new Employee("黎明",45,6500),
new Employee("郭富城",51,5500));
//过滤出年龄大于35
//内部迭代:迭代操作由StreamAPI完成
@Test
public void test1(){
//中间操作,不会执行任何操作
Stream<Employee> s = employees.stream().filter(e -> e.getAge()>35);
/*
s.forEach(System.out::println);这样写会打印对象所有信息,不会只打印姓名,而我只打印姓名
可以参考这样写Stream<String> namesStream = employees.stream().map(Employee::getName);
Stream<String> namesStream = s.map(employees -> employees.getName());//用Employee::getName更简洁
namesStream.forEach(System.out::println);
*/
//中间操作后必须加终止操作才执行
s.forEach((str) -> System.out.println(str.getName()));
/*
刘德华
黎明
郭富城
*/
}
//外部迭代
@Test
public void test2(){
Iterator<Employee> it = employees.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
@Test
public void test3(){
/*Employee [id=0, name=刘德华, age=48, salary=8000.0, status=null]
Employee [id=0, name=张学友, age=28, salary=8500.0, status=null]
Employee [id=0, name=黎明, age=45, salary=6500.0, status=null]*/
employees.stream().filter(e -> e.getSalary()>6000).limit(3).forEach(System.out::println);
System.out.println("------------------------------------------------------");
/*短路!
Employee [id=0, name=刘德华, age=48, salary=8000.0, status=null]
短路!
Employee [id=0, name=张学友, age=28, salary=8500.0, status=null]*/
//找到满足条件的两条数据后,后续的迭代操作就不再进行了
employees.stream().filter(e -> {
System.out.println("短路!");
return e.getSalary()>6000;
}).limit(2).forEach(System.out::println);
}
/*Employee [id=0, name=黎明, age=45, salary=6500.0, status=null]*/
//跳过前两个
@Test
public void test4(){
employees.stream().filter(e -> e.getSalary()>6000).skip(2).forEach(System.out::println);
}
/*Employee [id=0, name=刘德华, age=48, salary=8000.0, status=null]
Employee [id=0, name=张学友, age=28, salary=8500.0, status=null]
Employee [id=0, name=黎明, age=45, salary=6500.0, status=null]*/
//去重,注意要去实体类中重写hashcode和equals
@Test
public void test5(){
employees.stream().filter(e -> e.getSalary()>6000).distinct().forEach(System.out::println);
}
}