Java.util.TreeSet Class
Java TreeSet Class
Java.util package provides a TreeSet class which is 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. The class guarantees that the Map will be in ascending key order and backed by a TreeMap.
Class declaration
The declaration of java.util.TreeSet class is:
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
Class Constructors
S.N | Constructors & Description |
---|---|
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. |
java.util.TreeSet Methods
The java.util.TreeSet class has a number of methods which are listed below:
Member Methods
S.N | Methods & Description |
---|---|
1. | boolean add(E element) Adds the specified element to this set if it is not already present. |
2. | boolean addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set. |
3. | E ceiling(E e) Returns the least element in this set greater than or equal to the given element, or null if there is no such element. |
4. | void clear() Removes all of the elements from this set. |
5. | Object clone() Returns a shallow copy of this TreeSet instance. |
6. | Comparator<? super E> comparator() Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements. |
7. | boolean contains(Object o) Returns true if this set contains the specified element. |
8. | Iterator<E> descendingIterator() Returns an iterator over the elements in this set in descending order. |
9. | NavigableSet<E> descendingSet() Returns a reverse order view of the elements contained in this set. |
10. | E first() Returns the first (lowest) element currently in this set. |
11. | E floor(E e) Returns the greatest element in this set less than or equal to the given element, or null if there is no such element. |
12. | SortedSet<E> headSet(E toElement) Returns a view of the portion of this set whose elements are strictly less than toElement. |
13. | NavigableSet<E> headSet(E toElement, boolean inclusive) Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. |
14. | E higher(E e) Returns the least element in this set strictly greater than the given element, or null if there is no such element. |
15. | boolean isEmpty() Returns true if this set contains no elements. |
16. | Iterator<E> iterator() Returns an iterator over the elements in this set in ascending order. |
17. | E last() Returns the last (highest) element currently in this set. |
18. | E lower(E e) Returns the greatest element in this set strictly less than the given element, or null if there is no such element. |
19. | E pollFirst() Retrieves and removes the first (lowest) element, or returns null if this set is empty. |
20. | E pollLast() Retrieves and removes the last (highest) element, or returns null if this set is empty. |
21. | boolean remove(Object obj) Removes the specified element from this set if it is present. |
22. | int size() Returns the number of elements in this set (its cardinality). |
23. | Spliterator<E> spliterator() Creates a late-binding and fail-fast Spliterator over the elements in this set. |
24. | NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) Returns a view of the portion of this set whose elements range from fromElement to toElement. |
25. | SortedSet<E> subSet(E fromElement, E toElement) Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. |
26. | SortedSet<E> tailSet(E fromElement) Returns a view of the portion of this set whose elements are greater than or equal to fromElement. |
27. | NavigableSet<E> tailSet(E fromElement, boolean inclusive) Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement. |
Methods inherited
This class inherits the methods of following class:
- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractSet<E>