TreeMap is a class in the java.utils package that stores the keys in sorted order. Some of the features of TreeMap are:

  1. The entries in TreeMap are sorted in the natural ordering of its keys.

  2. It does not allow null keys, however there can be null values.

  3. 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.