TreeMap: Creation and Insertion
Let's look at the creation and insertion of TreeMap.
We'll cover the following
TreeMap is a class in the java.utils
package that stores the keys in sorted order. Some of the features of TreeMap are:
-
The entries in TreeMap are sorted in the natural ordering of its keys.
-
It does not allow null keys, however there can be null values.
-
The TreeMap is not thread-safe, although it can be made thread-safe using the
synchronizedMap()
method of the Collections class.
Since a TreeMap stores the keys in sorted order, the objects that we are storing in the TreeMap should either implement the Comparable interface or we should pass a Comparator while creating the TreeMap object.
Get hands-on with 1400+ tech skills courses.