TreeSet Class in Collection

public class TreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable
A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

Note that this implementation is not synchronized. If multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSortedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

The TreeSet class supports four constructors. The first form constructs an empty tree set that will be sorted in ascending order according to the natural order of its elements:
TreeSet( )
//The second form builds a tree set that contains the elements of c.
TreeSet(Collection c)

//The third form constructs an empty tree set that will be sorted according to the comparator specified by comp.
TreeSet(Comparator comp)

//The fourth form builds a tree set that contains the elements of ss:
TreeSet(SortedSet ss)
Methods in TreeSet:
SNMethods with Description
1void add(Object o)
Adds the specified element to this set if it is not already present.
2boolean addAll(Collection c)
Adds all of the elements in the specified collection to this set.
3void clear()
Removes all of the elements from this set.
4Object clone()
Returns a shallow copy of this TreeSet instance.
5Comparator comparator()
Returns the comparator used to order this sorted set, or null if this tree set uses its elements natural ordering.
6boolean contains(Object o)
Returns true if this set contains the specified element.
7Object first()
Returns the first (lowest) element currently in this sorted set.
8SortedSet headSet(Object toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement.
9boolean isEmpty()
Returns true if this set contains no elements.
10Iterator iterator()
Returns an iterator over the elements in this set.
11Object last()
Returns the last (highest) element currently in this sorted set.
12boolean remove(Object o)
Removes the specified element from this set if it is present.
13int size()
Returns the number of elements in this set (its cardinality).
14SortedSet subSet(Object fromElement, Object toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
15SortedSet tailSet(Object fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

Example of TreeSet class:
The following program illustrates several of the methods supported by this collection:
import java.util.*;

public class TreeSetDemo {

   public static void main(String args[]) {
      // Create a tree set
      TreeSet ts = new TreeSet();
      // Add elements to the tree set
      ts.add("C");
      ts.add("A");
      ts.add("B");
      ts.add("E");
      ts.add("F");
      ts.add("D");
      System.out.println(ts);
   }
}
output:

Example of TreeSet adding duplicate value:
import java.util.*;

public class TreeSetDemo {

   public static void main(String args[]) {
      // Create a tree set
      TreeSet ts = new TreeSet();
      // Add elements to the tree set
      ts.add("Dinesh");
      ts.add("Anamika");
      ts.add("Sweety");
      ts.add("Anamika");
      ts.add("Sweety");
      ts.add("Raj");
      System.out.println(ts);
   }
}
output:




<<Previous <<   || Index ||   >>Next >>


No comments:

Post a Comment