博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Java 8 Lambda表达式对Employee类进行操作
阅读量:6371 次
发布时间:2019-06-23

本文共 3875 字,大约阅读时间需要 12 分钟。

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
list = Arrays.asList(employees); // 打印全部的Employee信息 System.out.println("Complete Employee List: "); list.stream().forEach(System.out::println); System.out.println("-------------------------------"); // 定义函数式接口,返回boolean值,指定工资范围在大于等于4000小于等于6000的区间。 Predicate
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
byFirstName = Employee::getFirstName; Function
byLastName = Employee::getLastName; // 指定比较器比较规则 Comparator
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); }}

 

转载地址:http://xpuqa.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
忘记root用户密码怎么办?
查看>>
esxi定时任务
查看>>
Scaffold-DbContext
查看>>
关于VMware Workstation主机列表问题求教
查看>>
配置管理小报101021:给ubuntu加监控
查看>>
qml文字滚动效果的封装,实现方式运用的qml中提供的动画效果,另一种实现方式也可以使用定时器修改控件的坐标来实现...
查看>>
标准C++实现任务队列
查看>>
jdbc url
查看>>
刷leetcode第704题-二分查找
查看>>
debug_backtrace() 函数生成一个 backtrace(追踪)
查看>>
第七天,还是盒子
查看>>
XAMPP软件包下载
查看>>
XXL-JOB初体验-ORACLE版
查看>>
沉思录:别人的棺材
查看>>
jersey + spring + mybatis + redis项目搭建
查看>>
PAT 1006 部分正确_另一种解法
查看>>
在Keil环境下使用JLink实现printf输出重定向至debug窗口
查看>>
postgres的\d命令不显示全部的用户表
查看>>
poj 3468 A Simple Problem with Integers
查看>>