基本类Employee
package com.neuedu.java2;
public class Employee {
private int id;
private String name;
private int age;
private double 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() {
System.out.println("Employee().....");
}
public Employee(int id) {
this.id = id;
System.out.println("Employee(int id).....");
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
if (id != employee.id)
return false;
if (age != employee.age)
return false;
if (Double.compare(employee.salary, salary) != 0)
return false;
return name != null ? name.equals(employee.name) : employee.name == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
temp = Double.doubleToLongBits(salary);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
造数据工具类
package com.neuedu.java2;
import java.util.ArrayList;
import java.util.List;
/**
* 造数据
*/
public class EmployeeData {
public static List<Employee> getEmployees(){
List<Employee> list = new ArrayList<>();
list.add(new Employee(1,"马哥",34,11111));
list.add(new Employee(2,"里哥大王",13,2222));
list.add(new Employee(3,"刘哥",18,33333));
list.add(new Employee(4,"等哥大拿",99,4444));
list.add(new Employee(5,"啊哥",55,555));
return list;
}
}
正文开始测试
package com.neuedu.testListMerage;
import com.neuedu.java2.Employee;
import com.neuedu.java2.EmployeeData;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo01 {
@Test
public void test01(){
//创造数据
List<Employee> employees = EmployeeData.getEmployees();
//过滤出年龄为55和34的数据
List<Employee> collect = employees.stream().filter(employee -> employee.getAge() == 55 || employee.getAge() == 34)
.collect(Collectors.toList());
collect.forEach(System.out::println);
//将过滤后的数据 与 源数据进行合并
//注意:Stream.of(collect, employees) 以第一个参数为基准,去合并
List<Employee> list = Stream.of(collect, employees).flatMap(Collection::stream).distinct().collect(Collectors.toList());
System.out.println("_________________________");
list.forEach(System.out::println);
}
}
运行效果
distinct去重
flatMap将每个元素换成流,合成新的流。(联想记忆:addAll方法)