TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order.
TreeMap is an implementation of Map Interface . TreeMap is also Implementation of NavigableMap along with AbstractMap class.
Similarities between TreeSet and TreeMap in java.
- Both TreeMap and TreeSet belong to java.util package.
- Both are the part of the Java Collection Framework.
- They do not allow null values.
- Both are sorted. Sorted order can be natural sorted order defined by Comparable interface or custom sorted order defined by Comparator interface.
- They are not synchronized by which they are not used in concurrent applications.
- Both Provide O(log(n)) time complexity for any operation like put, get, containsKey, remove.
- Both TreeSet and TreeMap Internally uses Red-Black Tree.
Illustration of TreeMap and TreeSet:
Output[4, 5, 6, 8]
{2=3, 3=6, 4=6}