使用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);
    }
}

 

posted @ 2015-08-18 17:07  林本托  阅读(876)  评论(0编辑  收藏  举报