使用Java 8 Lambda表达式对Employee类进行操作
1,首先定义Employee类。
package coffee.how.to.program.early.objects.chapter15; public class Employee { private String firstName; private String lastName; private double salary; private String department; // constructor public Employee(String firstName, String lastName, double salary, String department) { this.firstName = firstName; this.lastName = lastName; this.salary = salary; this.department = department; } // set firstName public void setFirstName(String firstName) { this.firstName = firstName; } // get firstName public String getFirstName() { return firstName; } // set lastName public void setLastName(String lastName) { this.lastName = lastName; } // get lastName public String getLastName() { return lastName; } // set salary public void setSalary(double salary) { this.salary = salary; } // get salary public double getSalary() { return salary; } // set department public void setDepartment(String department) { this.department = department; } // get department public String getDepartment() { return department; } // return Employee's first and last name combined public String getName() { return String.format("%s %s", getFirstName(), getLastName()); } // return a String containing the Employee's information @Override public String toString() { return String.format("%-8s %-8s %8.2f %s", getFirstName(), getLastName(), getSalary(), getDepartment()); } // end method toString } // end class Employee
2,对Employee类进行操作类。
package coffee.how.to.program.early.objects.chapter15; /** * 定义若干 Employee 实例并加入数组, * 把数组转换成 list, * 根据 Employee的Last Name 和 First Name 定义比较器。 * 使用 lambda 和 stream 对 Employee 类进行操作。 */ import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.function.Function; import java.util.function.Predicate; public class ProcessingEmployees { public static void main(String[] args) { // 定义Employee类数组 Employee[] employees = { new Employee("Jason", "Red", 5000, "IT"), new Employee("Ashley", "Green", 7600, "IT"), new Employee("Matthew", "Indigo", 3587.5, "Sales"), new Employee("James", "Indigo", 4700.77, "Marketing"), new Employee("Luke", "Indigo", 6200, "IT"), new Employee("Jason", "Blue", 3200, "Sales"), new Employee("Wendy", "Brown", 4236.4, "Marketing") }; // 转换成list List<Employee> list = Arrays.asList(employees); // 打印全部的Employee信息 System.out.println("Complete Employee List: "); list.stream().forEach(System.out::println); System.out.println("-------------------------------"); // 定义函数式接口,返回boolean值,指定工资范围在大于等于4000小于等于6000的区间。 Predicate<Employee> four2SixThousand = e -> (e.getSalary() >= 4000 && e.getSalary() <= 6000); // 使用 stream 过滤,排序,再循环遍历打印。 list.stream().filter(four2SixThousand).sorted(Comparator.comparing(Employee::getSalary)) .forEach(System.out::println); // 打印比较器区间工资最小的Employee信息 System.out.printf("%nFirst employee who earns $4000-$6000:%n%s%n", list.stream().filter(four2SixThousand).min(Comparator.comparing(Employee::getSalary)).get()); Function<Employee, String> byFirstName = Employee::getFirstName; Function<Employee, String> byLastName = Employee::getLastName; // 指定比较器比较规则 Comparator<Employee> lastThenFistComp = Comparator.comparing(byLastName).thenComparing(byFirstName); // 先根据last name 比较,如果相同,再比较 first name。 System.out.printf("%nEmployees in ascending order by last name then fist name: %n"); list.stream().sorted(lastThenFistComp).forEach(System.out::println); //先根据first name 比较,如果相同,再比较 last name。 System.out.printf("%nEmployees in descending order by last name then first:%n"); list.stream().sorted(lastThenFistComp.reversed()).forEach(System.out::println); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?