Suggested Pages

Tuesday, January 31, 2017

Comparable vs Comparator

In Java programming one of the most important things to understand is the divide et impera concept. When you develop an algoritm that has to sort a data structure, it's important to divide these two concepts:
  • Sorting Logic
  • Structure Data Rappresentation
In Java there are two commons way to perform an sorting algoritm: using Comparator or using Comparable. Both these mechanisms aim to focus on the logic of the ordering, so developers should not have to think on the data structure to sort.
The main difference between Comparable and Comparator is that: the former is the natural way to sort a class, the latter is the unusual way to sort a class. Comparator is usually used for a specific scope in your application; if we have to implement the same concept in two different applications, we will implement probably the same bean class with the same implementation of Comparable. Instead we will write different Comparator classes in these two applications because Comparator is typically useful when there are multiple ways of comparing two instances of a certain type.

Comparable


Comparable is an interface that imposes total ordering, called Natural Ordering, on the objects. Comparable exposes int compareTo(T o) method that developers must implement in Comparable subclasses. Developers also have to be sure that its implementation is consistent with equals method at this way: o1.compareTo(o2) == 0 must have the same boolean value as o1.equals(o2).
Comparable interface is very important because if you want to sort a data structure as a List, you can use Collections.sort(List list) method. Collections class uses the Natural Ordering to sort data elements of the structure, therefore it take into consideration the way you implement int compareTo(T o) method.
In the following code there's an example of Archer class that implements public int compareTo(T t) of Comparable interface. You can notice that this method is perfectly consistent with equals method.

Archer.java
   public class Archer implements Comparable {

 private String name;
 
 private String company;
 
  
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getCompany() {
  return company;
 }

 public void setCompany(String company) {
  this.company = company;
 }

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

        @Override
 public int compareTo(Archer archer) {
  if (archer == this) {
   return 0;
  }
  return this.name.compareTo(archer.getName());
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Archer other = (Archer) obj;

  if (name == null) {
   if (other.name != null) {
    return false;
   }
  } else if (!name.equals(other.name)) {
   return false;
  }

  return true;
 }

 
 @Override
 public int hashCode() {
  return this.getName().hashCode()+this.company.hashCode();
 }

}


DataOrdering.java

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


public class DataOrdering {

 
 public static void order(Listarchers){
  Collections.sort(archers);
 }
}


Comparator


Comparator in an interface that has int compare(T o1, T o2) method.
As said above, Collections class in addition to using the natural ordering of sorting a list, it uses a method that takes as a parameter the Comparator object ( Collections.sort (List<T> list, Comparator <? super T>comparator ). In the following code there's an example of ArcheryCompanyComparator class that implements int compare(T o1, T o2) of Comparator interface.
As you can see we implement public int compare(Archer a1, Archer a2) method, giving an ordering that is not the usual way to sort Archer by the name.

ArcheryCompanyComparator.java
import java.util.Comparator;

public class ArcheryCompanyComparator implements Comparator{

 @Override
 public int compare(Archer a1, Archer a2) {
  return a1.getCompany().compareTo(a2.getCompany());
 }

}

No comments :

Post a Comment

Suggested Pages