Collections
TreeSet is the implementation of SortedSet Interface in Java. It is used for storage. It doesn’t contains duplicate values.
Objects are stored in a sorted and ascending order.
It is implementation of a self-balancing binary search tree such as RED-BLACK tree hence operations like add, remove and search take O(Log n) time.
TreeSet is similar to hashset except that it sorts the elements in the ascending order while HashSet doesn’t maintain any order.
TreeSet is not synchronized, if multiple threads access a tree set concurrently, and one of the threads modifies the set, it must be synchronized hence to synchronize set, set should be “wrapped” using the Collections.synchronizedSortedSet method at the creation time, to prevent accidental unsynchronized access to the set.
TreeSet treeset = new TreeSet();
Set syncSet = Collections.synchronziedSet(treeset);
Published on March 27, 2019 04:20