Iterate TreeMap in Java

In this tutorial, we will learn how to Iterate TreeMap in Java.

TreeMap in Java provides an efficient way of storing key/value pairs in sorted order and allows rapid retrieval of the elements in map. The TreeMap class extends AbstractMap and implements the NavigableMap interface.
It creates maps stored in a tree structure. TreeMap is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time.

There are many ways to iterate TreeMap in Java. We can iterate TreeMap using Iterator or for loop or while loop etc. If we want to iterate using Iterator, then we need to get Iterator by calling the method iterator() on TreeMap . Let’s see this with an example.



In the above program, we have iterated TreeMap using an Iterator and for loop.

Related posts

Leave a Comment