Master java skills

Comparable vs Comparator

Comparable is used when we have one sorting logic. When we need multiple sorting logic, comparator should be

We have seen how to sort list objects using Comparable as well as Comparator interface. Let’s see the difference between two interfaces.

ComparableComparator
Belongs to java.lang packageBelongs to java.util package
Comparable needs to be implemented by the class itself whose objects we want to sort in a list.Comparator is a different class altogether which is passed along with the list which needs to be sorted in Collections.sort() method.
Comparable has compareTo() method which needs to be overridden.Comparator has compare() method which needs to be overridden.
Comparable provides a single logic of sorting objectsWe can write as many Comparator classes as we want. And thus, we can have multiple sorting logic.

Example of sorting ArrayList using Comparable

In the below example, we are going to create a list of singers and then sort them based on their year of birth. We are going to follow natural sorting of integers, therefore the singer with earliest year of birth will be the first singer in the list. In the below example, Mohd Rafi is going to be the first singer in the list. And why not, he deserves to be the first (at least in this example).

package com.javatrainingschool;

public class Singer implements Comparable<Singer> {
	
	private String name;
	private int yearOfBirth;
	
	public Singer(String name, int yearOfBirth) {
		super();
		this.name = name;
		this.yearOfBirth = yearOfBirth;
	}

	@Override
	public String toString() {
		return "Singer [name=" + name + ", yearOfBirth=" + yearOfBirth + "]";
	}

	@Override
	public int compareTo(Singer s) {
		
		if(this.yearOfBirth > s.yearOfBirth)
			return 1;
		else if (this.yearOfBirth == s.yearOfBirth)
			return 0;
		else
			return -1;
	}
}
package com.javatrainingschool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SingerMain {
	
	public static void main(String[] args) {
		
		Singer s1 = new Singer("Kishor Kumar", 1929);
		Singer s2 = new Singer("Mohd Rafi", 1924);
		Singer s3 = new Singer("Asha Bhosle", 1933);
		Singer s4 = new Singer("Lata Mangeshkar", 1929);
		
		List<Singer> singers = new ArrayList<Singer>();
		singers.add(s1);
		singers.add(s2);
		singers.add(s3);
		singers.add(s4);
		
		System.out.println("Before sorting : " + singers);
		Collections.sort(singers);
		System.out.println("Sorted on year of birth : " + singers);
	}
}
Output :
Before sorting : [Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Mohd Rafi, yearOfBirth=1924], Singer [name=Asha Bhosle, yearOfBirth=1933], Singer [name=Lata Mangeshkar, yearOfBirth=1929]]

Sorted on year of birth : [Singer [name=Mohd Rafi, yearOfBirth=1924], Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Lata Mangeshkar, yearOfBirth=1929], Singer [name=Asha Bhosle, yearOfBirth=1933]]

Example of sorting ArrayList using Comparator

In the above example we saw that we sorted list of singers based on their year of birth. The same example we will do using Comparator interface.

package com.javatrainingschool;

public class Singer {
	
	private String name;
	private int yearOfBirth;
	
	public Singer(String name, int yearOfBirth) {
		super();
		this.name = name;
		this.yearOfBirth = yearOfBirth;
	}

	@Override
	public String toString() {
		return "Singer [name=" + name + ", yearOfBirth=" + yearOfBirth + "]";
	}
//getters and setters methods	

}

Comparator class that has logic to sort singers based on their year of birth

package com.javatrainingschool;

import java.util.Comparator;

public class SingerYOBComparator implements Comparator<Singer>{
	
	public int compare(Singer s1, Singer s2) {
		if(s1.getYearOfBirth() > s2.getYearOfBirth())
			return 1;
		else if (s1.getYearOfBirth() == s2.getYearOfBirth())
			return 0;
		else
			return -1;
	}
}

Class with main method

package com.javatrainingschool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SingerMain {
	
	public static void main(String[] args) {
		
		Singer s1 = new Singer("Kishor Kumar", 1929);
		Singer s2 = new Singer("Mohd Rafi", 1924);
		Singer s3 = new Singer("Asha Bhosle", 1933);
		Singer s4 = new Singer("Lata Mangeshkar", 1929);
		
		List<Singer> singers = new ArrayList<Singer>();
		singers.add(s1);
		singers.add(s2);
		singers.add(s3);
		singers.add(s4);
		
		System.out.println("Before sorting : " + singers);

                
                //Here we have to pass a new object of our comparator class
		Collections.sort(singers, new SingerYOBComparator());
		System.out.println("Sorted on year of birth : " + singers);
	}
}
Output :
Before sorting : [Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Mohd Rafi, yearOfBirth=1924], Singer [name=Asha Bhosle, yearOfBirth=1933], Singer [name=Lata Mangeshkar, yearOfBirth=1929]]

Sorted on year of birth : [Singer [name=Mohd Rafi, yearOfBirth=1924], Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Lata Mangeshkar, yearOfBirth=1929], Singer [name=Asha Bhosle, yearOfBirth=1933]]

Another comparator that has logic to compare singer based on their names in alphabetical order.

package com.javatrainingschool;

import java.util.Comparator;

public class SingerNameComparator implements Comparator<Singer>{
	
	public int compare(Singer s1, Singer s2) {
		return s1.getName().compareTo(s2.getName());
	}
}

If we compare the same list using SingerNameComparator, the result will be like below

Output :
Before sorting : [Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Mohd Rafi, yearOfBirth=1924], Singer [name=Asha Bhosle, yearOfBirth=1933], Singer [name=Lata Mangeshkar, yearOfBirth=1929]]

Sorted on singer name : [Singer [name=Asha Bhosle, yearOfBirth=1933], Singer [name=Kishor Kumar, yearOfBirth=1929], Singer [name=Lata Mangeshkar, yearOfBirth=1929], Singer [name=Mohd Rafi, yearOfBirth=1924]]

Since, we have done using some examples here remembering some singing legengs, it’s time to enjoy a cup of coffee in their names.