TreeSet



TreeSet is available since Java 1.2.

TreeSet extends AbstractSet class.

TreeSet implements following interfaces:
NavigableSet<E>
Cloneable
Serializable

TreeSet has four constructors:
TreeSet()
TreeSet(Collection<? extends E> c)
TreeSet(Comparator<? super E> comparator)
TreeSet(SortedSet<E> s)

Characteristics:
Internally TreeSet use TreeMap data structure and TreeMap uses the same HashMap mechanism to store the data but it store data as Tree Structure using the Entry class.
In TreeMap the Entry class has following members
        K key;
        V value;
        Entry<K,V> left = null;
        Entry<K,V> right = null;
        Entry<K,V> parent.
So using this members it stores the objects in the TreeSet using the HashMap mechanism.
  • By default TreeSet sort the data in natural order.
  • User can customize the ordering of data using the TreeSet(Comparator comp) constructor by passing the required Comparator as argument.
  • TreeSet allow null entry if there is  TreeSet is empty but if there are elements availabe in TreeSet then it wont allow null entry.
  • By default TreeSet does not allow duplicate elements. (Note: but user can pass duplicate elements into TreeSet if they prepare the comparator in that way).
  • While inserting elements into TreeSet, It maintains a Binary based on the integer value returned by the comparator compare() or default compareTo() method.
  • Insertion operation is slower in TreeSet, as it need to perform comparison on inserting element with the existing elements untill it get proper position in the Set.
  • The iterator's returned by the TreeSet iterator method is fail-fast.  
fail-fast:
It means if the TreeSet is modified at any time after the iterator is created by the TreeSet, then in any way except the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Here iterator can perform remove operation on the TreeSet but if the remove operation perform by something else it will throw the ConcurrentModificationException exception.

How to add duplicate element into TreeSet ?
You can insert duplicate elements in TreeSet by preparing a comparator with compare(ob1, ob2) method which will return only positive value or negativge value for all call on that method. It will never return zero for equals objects. then it will add the duplicate values in the TreeSet.

How to get index of particular element in the TreeSet?

To get index you can use headSet method which return the subSet of tree elements which are less than the element passed in the argument of headSet(element) method.
then call the size() method on the returned subSet of elements it will give you the index of that particular element.
set.headSet(element).size()

Example:
import java.util.*;

class TreeFlight {
public static void main(String args[]) {

TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(100);
ts.add(56);
ts.add(10);
ts.add(100); // duplicate not allowed
ts.add(11);               
System.out.println(ts);

}

}

Output
[10, 11, 56, 100]