TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and 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.
Following is the list of the constructors supported by the TreeSet class.
Sr.No. Constructor & Description 1TreeSet( )
This constructor constructs an empty tree set that will be sorted in an ascending order according to the natural order of its elements.
2TreeSet(Collection c)
This constructor builds a tree set that contains the elements of the collection c.
3TreeSet(Comparator comp)
This constructor constructs an empty tree set that will be sorted according to the given comparator.
4TreeSet(SortedSet ss)
This constructor builds a TreeSet that contains the elements of the given SortedSet.
Apart from the methods inherited from its parent classes, TreeSet defines the following methods −
Sr.No. Method & 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.
ExampleThe 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); } }
This will produce the following result −
Output[A, B, C, D, E, F]
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4