Core concept of Comparable and Comparator interface in Java | interview question on Java collection

package com.rajeev;


1. By Using comparable interface sorting logic(compareTo(Object obj)) implements within the class
so every time when you want to change sorting logic then there must be change inside the class. suppose
in below code right now sorting logic is based on employee's name when you want to changed it to based on employee Id then you
need to change logic as below

 @Override
       public int compareTo(Object obj) {
              emplyee emp=(emplyee)obj;
             return Integer.toString(this.employeeId).compareTo(Integer.toString(emp.getEmployeeId()));
       }

import java.util.*;
import java.util.Iterator;
import java.util.TreeSet;
class emplyee implements Comparable

{
       String name;
       int employeeId;
       public emplyee(String name, int employeeId )
       {
              this.name=name;
              this.employeeId=employeeId;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public int getEmployeeId() {
              return employeeId;
       }
       public void setEmployeeId(int employeeId) {
              this.employeeId = employeeId;
       }
        public int compareTo(Object obj) {
              emplyee emp=(emplyee)obj;
             return this.name.compareTo(emp.getName());
       }
       /*@Override
       public int compareTo(Object obj) {
              emplyee emp=(emplyee)obj;
             return Integer.toString(this.employeeId).compareTo(Integer.toString(emp.getEmployeeId()));
       }*/
}
public class comparableTest {
       public static void main(String [] args)
       {
              TreeSet ts= new TreeSet();
              ts.add(new emplyee("Humud",800));
              ts.add(new emplyee("rajeev",600));
              ts.add(new emplyee("aalu",100));
              //ts.add("rajeev");
              //ts.add("Aalu");
              Iterator it =ts.iterator();
              while(it.hasNext())
              {
                     emplyee em=(emplyee)it.next();
                     System.out.println("SOrted -------"+em.getName());
              }
       }
}


2. By Using Comparator interface you have to write to your sorting logic outside the
   class . so when you want change the sorting logic not need to change logic inside the emplyee class

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

class nameComparator implements Comparator
{
       @Override
       public int compare(Object obj1, Object obj2) {
              emplyee emp=(emplyee)obj1;
              emplyee emp2=(emplyee)obj2;
              return Integer.toString(emp.employeeId).compareTo(Integer.toString(emp2.getEmployeeId()));
       }
}
class emplyeeIdComparator implements Comparator
{
       @Override
       public int compare(Object obj1, Object obj2) {
              emplyee emp=(emplyee)obj1;
              emplyee emp2=(emplyee)obj2;
              return emp.getName().compareTo(emp2.getName());
       }
}
class emplyee
{
       String name;
       int employeeId;
       public emplyee(String name, int employeeId )
       {
              this.name=name;
              this.employeeId=employeeId;

       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public int getEmployeeId() {
              return employeeId;
       }
       public void setEmployeeId(int employeeId) {
              this.employeeId = employeeId;
       }
       /*@Override
       public int compareTo(Object obj) {
              emplyee emp=(emplyee)obj;
              return Integer.toString(this.employeeId).compareTo(Integer.toString(emp.getEmployeeId()));
       }*/
}
public class comparableTest {
       public static void main(String [] args)
       {
              TreeSet ts= new TreeSet(new emplyeeIdComparator());
              ts.add(new emplyee("Rumud",800));
              ts.add(new emplyee("rajeev",600));
              ts.add(new emplyee("aalu",100));
              //ts.add("rajeev");
              //ts.add("Aalu");
              Iterator it =ts.iterator();
              while(it.hasNext())
              {
                     emplyee em=(emplyee)it.next();
                     System.out.println("SOrted -------"+em.getName());
              }

       }
}



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.