这篇文章将帮助您了解Java 8中一些重要且经常使用的Stream操作,这使您使用Java编程变得容易。
让我们以传统示例为例,“员工和部门”。
public class Employee {private String name;private Integer age;private String city;private Department department;public Employee(String name, Integer age, String city, Department department) {this.name = name;this.age = age;this.city = city;this.department = department;}// getters and setters.}
public class Department {private String departmentName;private Integer noOfEmployees;public Department(String departmentName, Integer noOfEmployees) {this.departmentName = departmentName;this.noOfEmployees = noOfEmployees;}// getters and setters
}
我将按如下所示设置一些示例数据,以便向您展示Java 8 Stream接口的一些重要功能。 我们有四个部门,以及来自这些部门的一组员工。
Department account = new Department("Account", 75); Department hr = new Department("HR", 50);Department ops = new Department("OP", 25);Department tech = new Department("Tech", 150); List<Employee> employeeList = Arrays.asList(new Employee("David", 32, "Matara", account), new Employee("Brayan", 25, "Galle", hr),new Employee("JoAnne", 45, "Negombo", ops),new Employee("Jake", 65, "Galle", hr),new Employee("Brent", 55, "Matara", hr),new Employee("Allice", 23, "Matara", ops),new Employee("Austin", 30, "Negombo", tech),new Employee("Gerry", 29, "Matara", tech),new Employee("Scote", 20, "Negombo", ops),new Employee("Branden", 32, "Matara", account),new Employee("Iflias", 31, "Galle", hr));
查找所有居住在“ Matara”城市的员工,按姓名对他们进行排序,然后打印员工的姓名。
employeeList.stream().filter(e -> e.getCity().equalsIgnoreCase("Matara")).sorted(Comparator.comparing(Employee::getName)).forEach(e -> System.out.println(e.getName()));
查找员工工作的不同部门名称。
employeeList.stream().map(e -> e.getDepartment().getDepartmentName()).distinct().forEach(System.out::println);
查找这些员工所在的部门名称,该部门的员工人数超过50。
employeeList.stream().map(Employee::getDepartment).filter(d -> d.getNoOfEmployees() > 50).distinct().forEach(d -> System.out.println(d.getDepartmentName()));
创建一个逗号分隔的部门名称字符串,按字母顺序排序。
String s = employeeList.stream().map(e -> e.getDepartment().getDepartmentName()).distinct().sorted().reduce("", (a, b) -> (a + "," + b));
System.out.println(s);
人力资源部有员工吗?
if (employeeList.stream().anyMatch(e -> e.getDepartment().getDepartmentName().equalsIgnoreCase("HR"))) { System.out.println("Found employees frm HR department");
}
打印在会计部门工作的所有雇员的姓名。
employeeList.stream().filter(e -> e.getDepartment().getDepartmentName().equalsIgnoreCase("Account")).map(Employee::getName).forEach(System.out::println);
所有部门的最高员工人数是多少?
employeeList.stream().map(e -> e.getDepartment().getNoOfEmployees()).reduce(Integer::max).ifPresent(System.out::print);
查找员工人数最多的部门。
employeeList.stream().map(Employee::getDepartment).reduce( (d1, d2) -> d1.getNoOfEmployees() > d2.getNoOfEmployees() ? d1 : d2).ifPresent(d -> System.out.println(d.getDepartmentName()));
使用max()方法可以完成以下相同操作。
employeeList.stream().map(Employee::getDepartment).max(Comparator.comparing(Department::getNoOfEmployees)).ifPresent(d -> System.out.println(d.getDepartmentName()));
查找所有部门的员工总数。
employeeList.stream().map(e -> e.getDepartment().getNoOfEmployees()).distinct().reduce(Integer::sum).ifPresent(System.out::println);
翻译自: https://www.javacodegeeks.com/2018/07/java-8-stream-examples.html