112014Oct

TreeSet Class in java

TreeSet Class In Java:- TreeSet is backed up by NavigableMap in Java and by default it uses TreeMap.The elements are stored in sorted, ascending order according to natural order with uniqueness. We can construct a TreeSet with constructor that give the collection with own rules for what order we want by using a Comparable or Comparator. TreeSet class uses:-

public class TreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable

Every Collection implements :- Cloneable, java.io.Serializable interface. NavigableSet:-

public interface NavigableSet<E>
extends SortedSet<E>

A SortedSet extended with navigation methods reporting closest matches for given search targets. Methods lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element.

Constructors:-

1. TreeSet()

Constructs a new, empty tree set, sorted according to the natural ordering of its elements.

2. TreeSet(Collection<? extends E> c)

Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.

3. TreeSet(Comparator<? super E> comparator)

Constructs a new, empty tree set, sorted according to the specified comparator.

4. TreeSet(SortedSet<E> s)

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set. All elements inserted into the set must implement the Comparable interface directly or indirectly way.

Features of TreeSet:-

Duplicates:-All implements Set interface are not allowed to store duplicates.

Performance:- TreeSet provides guaranteed O(log(n)) time for common operations like add, remove and contains, while HashSet and LinkedHashSet offer constant time performance e.g. O(1) for add, contains and remove given hash function uniformly distribute elements in bucket.

Ordering : HashSet does not maintain any order while LinkedHashSet maintains insertion order of elements much like List interface and TreeSet maintains sorting order or elements.

null:-Treeset does not insert null value ,it throw java.lang.NullPointerException error .TreeSet uses compareTo() method of respective elements to compare them which throws NullPointerException while comparing with null.

Conclusion:-

  • TreeSet is sorted collection
  • Sorting of elements in TreeSet in natural order by default or we can customized sorting by using a Comparable or Comparato.
  • For large amount of data access and retrieval is faster with TreeSet.

When to Use TreeSet ,HashSet,LinkedHashSet:-

  • TreeSet is a SortedSet which stores elements in sorted order specified by using a Comparable or Comparator.
  • HashSet provide constant time operation for basic function like add,remove,search,contain,size.
  • LinkedHashSet is extension of HashSet where we want to maintain insertion order.LinkedHashSet creates copy of Set which contains same elements in same order.

set