Java 8 Java 8-具有嵌套对象的比较器

Java 8 Java 8-具有嵌套对象的比较器,java-8,comparator,functional-interface,Java 8,Comparator,Functional Interface,我正在编写一个比较器来比较两个employee对象 在这里,我试图比较两个基于部门的employee对象,后面分别是他们的姓名和id 我在这里面临的问题是与primitive的比较,它们的包装很简单,但是当我试图根据两名员工的部门比较他们时,我得到了以下编译错误: Employee类型未定义适用于此处的getDept(T) 根据我的理解,即使是部门getDept()也应该扩展为 getDept(本) 作为函数调用时,提供部门详细信息 代码如下: Employee.java package

我正在编写一个比较器来比较两个employee对象

在这里,我试图比较两个基于部门的employee对象,后面分别是他们的姓名和id

我在这里面临的问题是与primitive的比较,它们的包装很简单,但是当我试图根据两名员工的部门比较他们时,我得到了以下编译错误:

Employee类型未定义适用于此处的getDept(T)

根据我的理解,即使是部门getDept()也应该扩展为

  • getDept(本)

    作为函数调用时,提供部门详细信息

代码如下:

Employee.java

package com.deloitte.javatut.pojo;

public class Employee {

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    private String emptName;
    private Long empId;
    private Department dept;

    public String getEmptName() {
        return emptName;
    }

    public void setEmptName(String emptName) {
        this.emptName = emptName;
    }

    public Long getEmpId() {
        return empId;
    }

    public void setEmpId(Long empId) {
        this.empId = empId;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }

}
Department.java

package com.deloitte.javatut.pojo;
公共课系{

public Department() {
    // TODO Auto-generated constructor stub
}

private String deptName;
private Long deptId;

public String getDeptName() {
    return deptName;
}

public void setDeptName(String deptName) {
    this.deptName = deptName;
}

public Long getDeptId() {
    return deptId;
}

public void setDeptId(Long deptId) {
    this.deptId = deptId;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((deptId == null) ? 0 : deptId.hashCode());
    result = prime * result + ((deptName == null) ? 0 : deptName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Department other = (Department) obj;
    if (deptId == null) {
        if (other.deptId != null)
            return false;
    } else if (!deptId.equals(other.deptId))
        return false;
    if (deptName == null) {
        if (other.deptName != null)
            return false;
    } else if (!deptName.equals(other.deptName))
        return false;
    return true;
}
}

比较逻辑:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Department dept = new Department();
    dept.setDeptId(1L);
    dept.setDeptName("IT");
    Employee emp = new Employee();
    emp.setEmpId(2L);
    emp.setEmptName("John Doe");
    emp.setDept(dept);
    Employee emp2 = new Employee();
    emp2.setEmpId(4L);
    emp2.setEmptName("John Doe 2");
    emp2.setDept(dept);
    Function<Employee, Department> deptFunction = Employee::getDept;
    Comparator<Employee> empComparator = Comparator.comparing(Employee::getDept)
            .thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);

}
publicstaticvoidmain(字符串[]args){
//TODO自动生成的方法存根
部门=新部门();
部门设置部门(1L);
部门设置部门名称(“IT”);
员工emp=新员工();
emp.setEmpId(2L);
emp.setEmptName(“约翰·多伊”);
环境管理部;
员工emp2=新员工();
emp2.setEmpId(4L);
emp2.setEmptName(“John Doe 2”);
emp2.设置部门(部门);
函数deptFunction=Employee::getDept;
Comparator empComparator=Comparator.comparing(Employee::getDept)
.thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);
}

部门
没有实现可比,所以Java认为这是不可比的

要使这项工作正常进行,您可以使
部门
实施
可比


部门
未实施
可比
class Department implements Comparable<Department> {
   // ...
   public int compareTo(Department other) {
       // By "compare by department", you probably meant comparing by the department name, right?
       // If not, implement your own compareTo
       return getName().compareTo(other.getName()); 
   }
}
Comparator<Employee> empComparator = Comparator.comparing(x -> x.getDept().getName())
            .thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);