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

标签 java-8 comparator functional-interface

我正在编写一个比较器来比较两个员工对象。

在这里,我尝试根据部门比较两个员工对象,然后分别是他们的姓名和 ID。

我在这里面临的问题是与原语及其包装器的比较很简单,但是当我尝试根据他们的部门比较两个员工时,我收到以下编译错误:

The type Employee does not define getDept(T) that is applicable here

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

  • getDept(this)

    同时作为函数调用并提供部门详细信息。

代码如下:

员工.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;
    }

}

部门.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);

}

最佳答案

Department不实现Comparable<Department> ,所以 Java 认为没有可比性。

要实现此功能,您可以创建 Department实现Comparable<Department> :

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()); 
   }
}

或者,比较 comparing 中实际可比较的内容:

Comparator<Employee> empComparator = Comparator.comparing(x -> x.getDept().getName())
            .thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);

关于java-8 - Java 8 - 具有嵌套对象的比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048703/

相关文章:

java - 在接口(interface)的静态方法中调用默认方法

sql - SQL中的 "LIKE"和 "="有什么区别?

java - Java Stream::*Match 操作的通用声明

java - 如何按 boolean 字段对 ArrayList 对象进行排序

java - 为什么 this.getClass 给它自己的类名而不是匿名类名?

java - 在 Java 中使用 Stream 和 BinaryOperator 的 Fibonacci

java - Vert.x WebSocket 返回 200 而不是 101

java - 将 Lambda 转换为 Java 函数

java - 流收集累加器/组合器顺序

java - 为什么在 java.util.Comparator 中不强制执行 equals?