TreeMap

The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.

You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order.

The TreeMap class supports four constructors. The first form constructs an empty tree map that will be sorted by using the natural order of its keys:

  • TreeMap( ) – constructs an empty tree map that will be sorted by using the natural order of its keys.
  • TreeMap(Comparator comp) – constructs an empty tree-based map that will be sorted by using the Comparator comp.
  • TreeMap(Map m) -initializes a tree map with the entries from m, which will be sorted by using the natural order of the keys.
  • TreeMap(SortedMap sm) -initializes a tree map with the entries from sm, which will be sorted in the same order as sm.

TreeMap implements SortedMap and extends AbstractMap.

TreeMap allows null values and (one) null key.

Rate this post

Leave a Reply