Image Source Google Search

Comparable Interface

It is available in java.lang package. This interface contains only one method compareTo()

public int compareTo(Object obj)
obj1.compareTo(obj2)

return –ve if obj1 has to come before obj2.
+ ve if obj1 has to come after obj2.
0 if obj1 and obj2 are equal(Duplicate Objects).

Ex:

System.out.println("A".compareTo("Z")); à -ve
System.out.println("K".compareTo("A")); à_ve
System.out.println("K".compareTo("K")); à 0
System.out.println("a".compareTo("A")); à+ve
System.out.println("A".compareTo(new Integer(10))); à classCastException
System.out.println("A".compareTo(null)); à NullPointerException

Note: while Inserting the objects into the TreeSet JVM internally uses compareTo() method if we are depending on natural sorting order.
Sometimes we have to define our own customized sorting order, then we should go for comparator Interface.

Comparator Iterface

By using comparator object we can define our own customized sorting. Comparator Interface is available in java.util package. This interface defines the following 2 methods.

1) public int compare(Object obj1, Object obj2)
     returns –ve if obj1 has to come before obj2
                  +ve if obj1 has to come after obj2
                      0 if obj1 and obj2 are equal.



2) public boolean equals(Object obj)
When ever we are implementing comparator interface compulsory we should provide implementation for compare method. Implementing equals method is optional because it is already available from object class through inheritance.

Write a program to insert integer objects into the Treeset where the sorting order is descending order.

import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet(new MyComparator());
t.add(10);
t.add(5);
t.add(15);
t.add(20);
t.add(0);
System.out.println(t);
}
}

class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
Integer I1 = (Integer)obj1;
Integer I2 = (Integer)obj2;
if(I1<I2)
return +1;
else if (I1>I2)
return -1;
else
return 0;
}
}
O/P:- [20,15,10,5,0]

Flow of control in MyComparator

compare(10, 5) à 10, 5
compare(15, 10) à 15, 10, 5
compare(20, 15) à 20, 15, 10, 5
compare(0, 20)
compare(0, 15)
compare(0, 10)
compare(0, 5) à20, 15, 10, 5, 0

if we are not passing comparator object JVM will always call compareTo() method which is meant for default natural sorting order.

Case1: If we implement compare method as follows the outputs are

public int compare(Object obj1, Object obj2)
{
Integer I1 = (Integer)obj1;
Integer I2 = (Integer)obj2;
return I1.compareTo(I2); à [0,5,10,15,20]
return I2.compareTo(I1); à [20,15,10,5,0]
return -I1.compareTo(I2); à [20,15,10,5,0]
return -I2.compareTo(I1); à [0,5,10,15,20]
return I2-I1; à [20,15,10,5,0]
return I1-I2; à [0,5,10,15,20]
}

Case2: If we implement compare method as follows

public int compare(Object obj1, Object obj2)
{
return -1; à [0,20,15,5,10] (reverse of Insertion order)
return 1; à [10,5,15,20,0] (Insertion Order)
return 0; à [10] (All the remaining elements considered as duplicate objects)
}





Write a program to insert String Objects to the TreeSet Where the Sorting order is reverse of Alphabetical order.

import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet(new MyComparator());
t.add("chiru");
t.add("venky");
t.add("nagaruna");
t.add("balaiah");
t.add("saikumar");
t.add("suman");
System.out.println(t);
}
}

class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
String s1 = (String)obj1;
String s2 = (String)obj2;
return s2.compareTo(s1);
}
}


Post a Comment

 
Top