TreeSet class
The objects of the TreeSet class are stored in ascending order.
The important points about the Java TreeSet class are:
- Java TreeSet class access and retrieval times are quiet fast.
- Java TreeSet class doesn't allow null element.
- Java TreeSet class is non synchronized.
- Java TreeSet class maintains ascending order.
- The TreeSet can only allow those generic types that are comparable. For example The Comparable interface is being implemented by the StringBuffer class.
Internal Working of The TreeSet Class
TreeSet is being implemented using a binary search tree, which is self-balancing just like a Red-Black Tree. Therefore, operations such as a search, remove, and add consume O(log(N)) time. The reason behind this is there in the self-balancing tree. It is there to ensure that the tree height never exceeds O(log(N)) for all of the mentioned operations. Therefore, it is one of the efficient data structures in order to keep the large data that is sorted and also to do operations on it.
Synchronization of The TreeSet Class
the TreeSet class is not synchronized. It means if more than one thread concurrently accesses a tree set, and one of the accessing threads modify it, then the synchronization must be done manually. It is usually done by doing some object synchronization that encapsulates the set. However, in the case where no such object is found, then the set must be wrapped with the help of the Collections.synchronizedSet() method. It is advised to use the method during creation time in order to avoid the unsynchronized access of the set.
TreeSet treeSet = new TreeSet();
Set syncrSet = Collections.synchronziedSet(treeSet);
Constructors
TreeSet() : It is used to construct an empty tree set that will be sorted in ascending order according to the natural order of the tree set.
TreeSet(Collection<? extends E> c) : It is used to build a new tree set that contains the elements of the collection c.
TreeSet(Comparator<? super E> comparator) : It is used to construct an empty tree set that will be sorted according to given comparator.
TreeSet(SortedSet<E> s) : It is used to build a TreeSet that contains the elements of the given SortedSet.
Extra Methods
- E ceiling(E e) : It returns the equal or closest greatest element of the specified element from the set, or null there is no such element.
- Comparator<? super E> comparator() : It returns a comparator that arranges elements in order.
- Iterator descendingIterator() : It is used to iterate the elements in descending order.
- NavigableSet descendingSet() : It returns the elements in reverse order.
- E floor(E e) : It returns the equal or closest least element of the specified element from the set, or null there is no such element.
- SortedSet headSet(E toElement) : It returns the group of elements that are less than the specified element.
- NavigableSet headSet(E toElement, boolean inclusive) : It returns the group of elements that are less than or equal to(if, inclusive is true) the specified element.
- E higher(E e) : It returns the closest greatest element of the specified element from the set, or null there is no such element.
- E lower(E e) : It returns the closest least element of the specified element from the set, or null there is no such element.
- E pollFirst() : It is used to retrieve and remove the lowest(first) element.
- E pollLast() : It is used to retrieve and remove the highest(last) element.
- Spliterator spliterator() : It is used to create a late-binding and fail-fast spliterator over the elements.
- NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) : It returns a set of elements that lie between the given range.
- SortedSet subSet(E fromElement, E toElement)) : It returns a set of elements that lie between the given range which includes fromElement and excludes toElement.
- SortedSet tailSet(E fromElement) : It returns a set of elements that are greater than or equal to the specified element.
- NavigableSet tailSet(E fromElement, boolean inclusive) : It returns a set of elements that are greater than or equal to (if, inclusive is true) the specified element.
- E first() : It returns the first (lowest) element currently in this sorted set.
- E last() : It returns the last (highest) element currently in this sorted set.
Comments
Post a Comment