Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 - Comparator with nested objects

I am writing a comparator to compare two employee objects.

Here i am trying to compare two employee object based on their department ,followed by their name and id respectively.

The issue which i am facing here is the comparison with primitive and their wrappers is straightforward but when i am trying to compare two employee based on their department, i am getting following compilation error:

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

As per my understanding even Department getDept() should get expanded as

  • getDept(this)

    while getting invoked as a function and provide the department details.

The code is as follows:

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 class Department {

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

}

Comparision logic:

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

}
like image 756
Dharmvir Tiwari Avatar asked Mar 06 '23 04:03

Dharmvir Tiwari


1 Answers

Department does not implement Comparable<Department>, so Java does not think that is is comparable.

To make this work, you can either make Department implement 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()); 
   }
}

Alternatively, compare something that's actually comparable in comparing:

Comparator<Employee> empComparator = Comparator.comparing(x -> x.getDept().getName())
            .thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);
like image 98
Sweeper Avatar answered Apr 28 '23 19:04

Sweeper