Comparable and Comparator in Java:

What is Comparable and Comparator?
Comparable is an interface used for sorting the objects. It supports Single sorting only.
Comparator is an interface used for Sorting, but supports multiple sorting.

Difference Between Comparable and Comparator:

Comparable Comparator
Compatable interface is used for Single sorting and overrides CompareTo() Comparator interface is used for Multiple Sorting and overrides Compare()
java.lang.Comparable java.util.Comparator


Comparable Example:
In comparable, We select a single object and compare with others to sort it. In this example, We are going to select one number and compare it with other numbers and sort them.

 

import java.lang.Comparable;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;

/**
 *
 * @author Vikram
 */
public class SingleSorting implements Comparable<SingleSorting> {

    int number;

    public SingleSorting(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
    
    
    @Override
    public int compareTo(SingleSorting o) {
      if(this.number == o.getNumber()){
          return 0;
      }
      else if(this.number > o.getNumber()){
          return 1; // return -1 for descending order
      }
      else{
          return -1; // return 1 here for descending order
      }
    }
    
    public static void main(String args[]){
        List<SingleSorting> list1 = new LinkedList();
        list1.add(new SingleSorting(87));
        list1.add(new SingleSorting(99));
        list1.add(new SingleSorting(12));
        list1.add(new SingleSorting(60));
        list1.add(new SingleSorting(75));
        list1.add(new SingleSorting(7));
        
        Collections.sort(list1);
        
        list1.forEach(list -> System.out.println(list.number));    
    }   
}

Output:

comparable

Comparator Example:
In comparator, We select two objects and compare them with each other and make the sort.

import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
/**
 *
 * @author Vikram
 */
public class DoubleSorting implements Comparator<DoubleSorting> {

    int number;
    String name;

    public DoubleSorting(int number, String name) {
        this.number = number;
        this.name = name;
    }
    
    public DoubleSorting(){    
    }
    
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public int compare(DoubleSorting o1, DoubleSorting o2) {
        return o1.getNumber() - o2.getNumber();    
        //return o1.getName().compareTo(o2.getName());  --> Sorting based on name        
    }
    
    public static void main(String args[]){
        List<DoubleSorting> list1 = new LinkedList();
        list1.add(new DoubleSorting(24, "Charlie"));
        list1.add(new DoubleSorting(56, "Beta"));
        list1.add(new DoubleSorting(12, "Zebta"));
        list1.add(new DoubleSorting(16, "Eagle"));
        list1.add(new DoubleSorting(04, "Delta"));
        Collections.sort(list1, new DoubleSorting()); 
        list1.forEach(list -> System.out.println(list.number +" "+list.name));  
    } 
}

Output:
comparator

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *