Wednesday, April 1, 2009

treeset-Comparator

SortedSet Interface:
If you want to represent a group of individual,unique set of objects ,where all the objects are in some sorting order (either natural sorting order or customized sorting order) then we should go for SortedSet.
SortedSet methods:
1.Object first()
returns the first element in sortedset
2.Object last()
return the last element in sortedset.
3.SortedSet headset(Object end)
returns the sorted set containing the elements which are lessthan end.
4.SortedSet tailSet(Object begain)
returns the sorted set that includes the elements which are greaaer than or equal to begain.
5.SortedSet subset(Object begain,Object End)
return a sortedset that includes the elements which are greater than or equal to begain but less than end.
6.Comparator comparator()
decribes underlying sorting technique if default sorting technique is used then it simply returns null.

TreeSet class :

  • The underlying datastructure for the treeset is balancedtree.
  • Duplicate objects are not allowed.
  • Insertion order is not preserved ,but all the elements are arranged in some sorting order.
  • Null insersion is possible but only one.
  • Hetrogeous objects are not allowed ,voialtion leads to runtime exception saying classcast exception.
Constructors:
1.treeSet t=new TreeSet()
creates an empty TreeSet where the sorting technique is default natural order.
2.TreeSet t=new TreeSet(Comparator c)
creates an empty TreeSet ,where the sorting technique is specified by comparator object .( this is for customized sorting).
3.TreeSet t=new TreeSet(Collection c)
4.TreeSet t=new TreeSet(SortedSet s)
reserved for future purpose.

TreeSet Example:
import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t=new TreeSet();
t.add(“z”);
t.add(“k”);
t.add(“b”);
t.add(“f”);
System.out.println(t); // [b,f,k,z]
t.add(new Integer(10)); // class cast exception
}
}
In case of integers (10,15,20) it followes ascending order.
TreeSet t=new TreeSet()
t.add(null);
System.out.println(t);
t.add(“a”);
System.out.println(t); // nullpointer exception
Null acceptance:
For the empty TreeSet as the first element we are allowed to add null.
But after adding null if we are trying to add anyother ,we will get a RTE saying NullPointerException”.If the treeset already contains some elements,If we are trying to add null causes once again nullpointer Exception.

In case of StringBuffer objects
t.add(new StringBuffer(‘a”));
t.add(new StringBuffer(“b”));
t.add(new StringBuffer(‘c”));
t.add(new StringBuffer(“l”));
System.out.println(t); // RTE saying classcast exception

i.e In the treeset we should add only.
  • Homogeneous and comparable objects violation leads classcast Exception
  • An object is said to be comparable if an only if the corresponding class implements comparable interface.
  • String and Wrapper classes already implemented comparable interface.But StringBuffer doesn’t implemented comparable interface.
Comparator Interface:
If you want to define our own sorting we havwet to implement comparator interface.
This interface present in java.util package.
This interface contain the following two methods.
1.public int compare(Object o1,Object o2)
return –ve number if o1 comes before o2.
return +ve number If o1 comes after o2.
returns zero if o1 and o2 equal.
2.public Boolean equals(Object o)
by using comparator interface we can define our own customized sorting.

Write a program to insert integer object in the TreeSet where the Sorting technique is descending order.

import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t=new Treeset(new MyComparator());
t.add(new Integer(20));
t.add(new Integer(10));
t.add(new Integer(30));
t.add(new Integer(100));
System.out.println(t); // 10 ,20 ,30,100
}
}
class MyComparator implements Comparator
{
public int Compare(Object o1,Object o2)
{
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
int i1=i1.intValue();
int i2=i2.intValue();
if(i1>i2)
return -1;
else
if(i1 > i2)
return +1;
else
return 0;
}
}

write a program to insert string objects into the TreeSet where the sorting order is increasing order of String lengths.

import java.util.*;
class ComparatorDemo
{
public static void main(String arg[])
{
TreeSet t=new Treeset(new Mycomparator())
t.add(“aaaaaa”);
t.add(“bbbb”);
t.add(“ccc”);
System.out.println(t); // [ccc,bbbb,aaaaaa]
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=(String)o1;
String s2=(String)o2;
if(s1.length() < s2. length())
return -1;
else
if(s1.length() > s2.length())
return +1;
else
return 0;
}
}

program to insert StringBuffer Objects in the treeSet according to dictionary order

import java.util.*;
class ComparatorDemo
{
public static void main(String arg[])
{
TreeSet t=new TreeSet(new MyComparator());
t.add(new StringBuffer(“aaa”);
t.add(new StringBuffer(“bbb”);
t.add(new StringBuffer(“ccc”);
t.add(new StringBuffer(‘ddd”);
System.out.println(t);
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=o1.toString();
String s2=o2.toString();
return s1.compareTo(s2);
//return s2.compareTo(s1);
}
}

if we return always like this:
return 0; // aaa
return +100 // bbb,ccc,aaa,ddd
return -100 // ddd,aaa,ccc,bbb



1 comments:

Anonymous said...

nice explanation...thanks

Post a Comment