流处理的主要接口和类主要分为以上三个
image.pngpackage All.D14.Steam_;
import java.util.ArrayList;
import java.util.List;
public class Employee { // 员工类
private String name; // 姓名
private int age; // 年龄
private double salary; // 工资
private String sex; // 性别
private String dept; // 部门
// 构造方法
public Employee(String name, int age, double salary, String sex, String dept) {
this.name = name;
this.age = age;
this.salary = salary;
this.sex = sex;
this.dept = dept;
}
// 重写此方法,方便打印输出员工信息
public String toString() {
return "name=" + name + ", age=" + age + ", salary=" + salary + ", sex=" + sex + ", dept=" + dept;
}
// 以下是员工属性的getter方法
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getSex() {
return sex;
}
public String getDept() {
return dept;
}
public static List<Employee> getEmpList() { // 提供数据初始化方法
List<Employee> list = new ArrayList<Employee>();
// 添加员工数据
list.add(new Employee("老张", 40, 9000, "男", "运营部"));
list.add(new Employee("小刘", 24, 5000, "女", "开发部"));
list.add(new Employee("大刚", 32, 7500, "男", "销售部"));
list.add(new Employee("翠花", 28, 5500, "女", "销售部"));
list.add(new Employee("小马", 21, 3000, "男", "开发部"));
list.add(new Employee("老王", 35, 6000, "女", "人事部"));
list.add(new Employee("小王", 21, 3000, "女", "人事部"));
return list;
}
}
package All.D14;
import All.D14.Steam_.Employee;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Demon01 {
public static void main(String[] args) {
List<Employee> list = Employee.getEmpList(); // 获取公共类的测试数据
Stream<Employee> stream = list.stream(); // 获取集合流对象
// 将年龄大于30岁的员工过滤出来
stream = stream.filter(people -> people.getAge() > 30);
// 将流对象重新封装成一个List集合
List<Employee> result = stream.collect(Collectors.toList());
for (Employee emp : result) { // 遍历结果集
System.out.println(emp); // 输出员工对象信息
}
}
}
name=老张, age=40, salary=9000.0, sex=男, dept=运营部
name=大刚, age=32, salary=7500.0, sex=男, dept=销售部
name=老王, age=35, salary=6000.0, sex=女, dept=人事部
网友评论