Tuesday, April 24, 2012

Comparable vs. Comparator interface



  • Comparable vs. Comparator interface


Classes should implement the Comparable interface to control their natural ordering
Use Comparator to sort objects in an order other than their natural ordering.
http://grdurand.com/static/presentation_four/comparable.html



  • Use of comparable and comparator interface

A class implementing Comparable interface need to override compareTo(Object obj) method and put the logic for sorting
Comparator interface is used when an extra logic is required to sort the objects. One need to override compare(Object obj1, Object obj2) method
http://java-questions.com/use_of_comparable_comparator.html




  • How to use Comparator and Comparable in Java? With example

If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.

How to Compare String in Java
For comparing String in Java we should not be worrying because String implements Comparable interface in Java and provides implementation for CompareTo method which Compare two strings based on  characters inside or you can say in lexical order. You just need to call String.compareTo(AnotherString) and Java will determine whether specified String is greater than , equal to or less than current String. String is also immutable in Java an important property to remember.


How to Compare Dates in Java
Dates are represented by java.util.Date class in Java and like String Dates also implements Comparable in Java so they will be automatically sorted based on there natural ordering if they got stored in any sorted collection like TreeSet or TreeMap.

http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html#ixzz1swgniOFa





  • Lesson12 java lang Comparable

http://www.youtube.com/watch?v=6x_YG-27iwY&feature=related




  • “How you will sort Employee object based on his EmployeeID and his name” and this involves the use of both Comparable as well as Comparator interface in Java. 


often required to sort objects stored in any collection classes like ArrayList, HashSet or in Array and that time we need to use either  compare() or  compareTo() method defined in java.util.Comparator and java.lang.Comparable.


java.util.Comparator
Comparator interface in Java has method public int compare (Object o1, Object o2)
Comparator in Java compare two objects provided

java.lang.Comparable
public int compareTo(Object o)
Comparable interface compares "this" reference with the object specified
In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects


if you want to sort objects based on natural order then use Comparable in Java
if you want to sort on some other attribute of object then use Comparator in Java.

For a Person class, sorting based on person_id can be treated as natural order sorting and sorting based on name field can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.

http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html#ixzz2D3Lj5aSb



  • public boolean equals(Object obj)

This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked.
The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e.
It checks if x == y.
This particular comparison is also known as "shallow comparison".


public int hashCode()
This method returns the hash code value for the object on which this method is invoked.
This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must be overridden in every class that overrides the equals method
http://www.javaranch.com/journal/2002/10/equalhash.html




  • A comparator object is capable of comparing two different objects. 

The class is not comparing its instances, but some other class's instances.
This comparator class must implement the java.util.Comparator interface.
http://www.java-connect.com/collection-framework/Comparator-in-java.html




  • Understanding Java's Integer Pool Can Avoid Problems

Integer i1 = 128
Integer i2 = 128

If you then execute the test (i1 == i2), the returned result is false. The reason is that the JVM maintains a pool of Integer values (similar to the one it maintains for Strings). But the pool contains only integers from -128 to 127. Creating any Integer in that range results in Java assigning those Integers from the pool, so the equivalency test works. However, for values greater than 127 and less than -128), the pool does not come into play, so the two assignments create different objects, which then fail the equivalency test and return false.
http://www.devx.com/tips/Tip/42276