title: Java 8 - Comparator with nested objects
body: <p>I am writing a comparator to compare two employee objects.</p>
  <p>Here i am trying to compare two employee object based on their department ,followed by their name and id respectively.</p>
  <p>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:</p>
  <blockquote>
  <p>The type Employee does not define getDept(T) that is applicable here</p>
  </blockquote>
  <p>As per my understanding even <strong>Department getDept()</strong> should get expanded as</p>
  <ul>
  <li><p>getDept(this)</p>
  <p>while getting invoked as a function and provide the department details.</p>
  </li>
  </ul>
  <p>The code is as follows:</p>
  <p>Employee.java</p>
  <pre><code>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;
      }
  
  }
  </code></pre>
  <p>Department.java</p>
  <pre><code>package com.deloitte.javatut.pojo;
  </code></pre>
  <p>public class Department {</p>
  <pre><code>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;
  }
  </code></pre>
  <p>}</p>
  <p>Comparision logic:</p>
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      Department dept = new Department();
      dept.setDeptId(1L);
      dept.setDeptName(&quot;IT&quot;);
      Employee emp = new Employee();
      emp.setEmpId(2L);
      emp.setEmptName(&quot;John Doe&quot;);
      emp.setDept(dept);
      Employee emp2 = new Employee();
      emp2.setEmpId(4L);
      emp2.setEmptName(&quot;John Doe 2&quot;);
      emp2.setDept(dept);
      Function&lt;Employee, Department&gt; deptFunction = Employee::getDept;
      Comparator&lt;Employee&gt; empComparator = Comparator.comparing(Employee::getDept)
              .thenComparing(Employee::getEmpId).thenComparing(Employee::getEmptName);
  
  }
  </code></pre>