Monday, 29 June 2015

How to use Comparator and Comparable in Java? With example


1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that Comparator should be used as an utility to sort objects which Comparable should be provided by default.

2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

3) 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. I have shared lot of tips on how to override compareTo() method and avoid some common mistakes programmer makes while implementing Comparable interface.

4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.

5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using  Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.

6)Objects which implement Comparable in Java  can be used as keys in a SortedMap like TreeMap or elements in a SortedSet  for example TreeSet, without specifying any Comparator.

These were combination of some theoretical and practical differences between Comparator and Comparator interface in Java. It does help you to decide when to use Comparator vs Comparable but things will be more clear when we some best practices around using both of these interfaces. Now let’s see an example of Comparator in Java:

Example of using Comparator and Comparable in Java

So in Summary if you want to sort objects based on natural order then use Comparable in Java and if you want to sort on some other attribute of object then use Comparator in Java. Now to understand these concepts lets see an example or real life coding:


1) There is class called Person, sort the Person based on person_id, which is primary key in database
2) Sort the Person based on there name.

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.


public class Person implements Comparable {
    private int person_id;
    private String name;
 
    /**
     * Compare current person with specified person
     * return zero if person_id for both person is same
     * return negative if current person_id is less than specified one
     * return positive if specified person_id is greater than specified one
     */

    @Override 
    public int compareTo(Object o) {

        Person p = (Person) o; 
        return this.person_id - o.person_id ;
    }
    ….
}

Generally you should not use difference of integers to decide output of compareTo method as result of integer subtraction can overflow but if you are sure that both operands are positive then its one of the quickest way to compare two objects. See my post things to remember while overriding compareTo in Java for more tips on compareTo.

And for sorting based on person name we can implement compare(Object o1, Object o2) method of Java Comparator class.

/**
 * Comparator implementation which sorts Person objects on person_id field
 */

public class SortByPerson_ID implements Comparator{

    public int compare(Object o1, Object o2) {

        Person p1 = (Person) o;
        Person p2 = (Person) o; 
        return p1.getPersonId() - p2.getPersonId();
    }
}

Similar guidelines applies while implementing compare() method as well and instead of using subtraction operator, its better to use logical operator to compare whether two integers are equal to, less than or greater than. You can write several types of Java Comparator based upon your need for example  reverseComparator , ANDComparator , ORComparator etc which will return negative or positive number based upon logical results. String in Java even provides an special comparator called CASE_INSENSITIVE_ORDER, to perform case insensitive comparison of String objects.



How to Compare String in Java
String is immutable in Java and one of the most used value class. For comparing String in Java we should not be worrying because String implements Comparable interface and provides a lexicographic implementation for CompareTo method which compare two strings based on contents of characters 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 object. See my post 4 example to compare String in Java for alternatives ways of comparing String.


How to Compare Dates in Java
Dates are represented by java.util.Date class in Java and like String,  Date 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. If you explicitly wants to compare two dates in Java you can call Date.compareTo(AnotherDate) method in Java and it will tell whether specified date is greater than , equal to or less than current String. See my post 3 ways to compare Dates in Java for more alternatives of comparing two dates.

When to use Comparator and Comparable in Java
At last let’s see some best practices and recommendation on when to use Comparator or Comparable in Java:

1) If there is a natural or default way of sorting Object already exist during development of Class than use Comparable. This is intuitive and you given the class name people should be able to guess it correctly like Strings are sorted chronically, Employee can be sorted by there Id etc. On the other hand if an Object can be sorted on multiple ways and client is specifying on which parameter sorting should take place than useComparator interface. for example Employee can again be sorted on name, salary or department and clients needs an API to do that. Comparator implementation can sort out this problem.

2) Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects.

3) Beware with the fact that How those object will behave if stored in SorteSet or SortedMap like TreeSet and TreeMap. If an object doesn't implement Comparable than while putting them into SortedMap, always provided corresponding Comparator which can provide sorting logic.

4) Order of comparison is very important while implementing Comparable or Comparator interface. for example if you are sorting object based upon name than you can compare first name or last name on any order, so decide it judiciously. I have shared more detailed tips on compareTo on my post how to implement CompareTo in Java.

5) Comparator has a distinct advantage of being self descriptive  for example if you are writing Comparator to compare two Employees based upon there salary than name that comparator as SalaryComparator, on the other hand compareTo()

Difference between String literal and New String object in Java

String is a special class in Java API and has so many special behaviours which is not obvious

to many programmers. In order to master Java, first step is to master String class, and one 
way to explore is checking what kind of String related questions are asked on Java interviews.
 Apart from usual questions like why String is final, or  equals vs == operator, one of the most
frequently asked question is what is difference between String literal and String object in
 Java.For example, what is the difference between String object created in following two
 expression :
String strObject = new String("Java");
and
String strLiteral = "Java";
Both expression gives you String object, but there is subtle difference between them. When
you create String object using new() operator, it always create a new object in heap
 memory. On theother hand, if you create object using String literal syntax e.g. "Java",
 it may return an existing object from String pool (a cache of String object in Perm gen
 space, which is now moved to heap space in recent Java release), if it's already exists. 
Otherwise it will create a new string object and put in string pool for future re-use. In 
rest of this article, why it is one of the most important thing
 you should remember about String in Java.



What is String literal and String Pool

Since String is one of the most used type in any application, Java designer took a step further
to optimize uses of this class. They know that Strings will not going to be cheap, and that's
 why they come up with an idea to cache all String instances created inside double quotes
 e.g. "Java".These double quoted literal is known as String literal and the cache which 
stored these String instances are known as as String pool. In earlier version of Java, I think 
up-to Java 1.6 String pool is located in permgen area of heap, but in Java 1.7 updates its 
moved to main heap area. Earlier since it was in PermGen space, it was always a risk to 
create too many String object, because its a very limited space, default size 64 MB and used
 to store classmetadata e.g. .class files. Creating too many String literals can cause
 java.lang.OutOfMemory: permgen space. Now because String pool is moved to a much 
larger memory space, it's much more safe. By the way, don't misuse memory
here, always try to minimize temporary String object e.g. "a""b" and then "ab".
Always use StringBuilder to deal with temporary String object.


Difference between String literal and String objectString literal vs New String in Java

At high level both are String object, but main difference comes from the point that new()
operator always creates a new String object. Also when you create String using literal they 
are interned. This will be much more clear when you compare two String objects created 
using String literal and new operator, as shown in below example :

String a = "Java";
String b = "Java";
System.out.println(a == b);  // True

Here two different objects are created and they have different references:

String c = new String("Java");
String d = new String("Java");
System.out.println(c == d);  // False

Similarly when you compare a String literal with an String object created using new()
 operator using ==operator, it will return false, as shown below :

String e = "JDK";
String f =  new String("JDK");
System.out.println(e == f);  // False

In general you should use the string literal notation when possible. It is easier to read and
it gives  the compiler a chance to optimize your code. By the way any answer to this 
question is incomplete until you explain what is String interning, so let's see that in
 next section.

String interning using inter() method

Java by default doesn't put all String object into String pool, instead they gives you
flexibility to explicitly store any arbitrary object in String pool. You can put any object
to String pool by calling 
intern()method of java.lang.String class. Though,
 when you create using String  literal notation of Java, it automatically call intern()
 method to put that object into String pool, provided it was not present in the pool 
already. This is another difference between string literal and  new string, because in 
case of new, interning doesn't happen automatically, until you call intern() method
 on that object. Also don't forget to use StringBuffer and StringBuilder for
string concatenation, they will reduce number


That's all about this question, what is difference between String literal and String object 
in JavaAlways remember that literal Strings are returned from string pool and Java put
them in pool if not stored already. This difference is most obvious, when you compare two 
String objects using equality operator (==). That's why it's suggested as always compare 
two String object using equals() method and never compare them using == operator,
 because you never know which one is coming from pool and which one is created using
 new() operator. If you know the difference between string object and string literal, you
 can also solve questions from Java written test, which also test this concept. It's something, 
every Java programmer should know.  of temporary
String object in heap space.

Monday, 8 June 2015

Serialization in Java

JAVA: Serialization in Java: If a class is serializable, it means that object of that class can be converted into a sequence of bits so that it can be written to some s...

Difference between MyClass.class and this

JAVA: Difference between MyClass.class and this: this :  It refers to  the current instance of the class where you are working on. MyClass.class:  It refers to the Class instance tha...