Get size of TreeMap in Java

In this tutorial, we will learn how to Get size of  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. TreeMap in Java is not synchronized

In we want to calculate the size or no. of elements in TreeMap, then this class in Java provides size() method to get size of TreeMap. The size method returns the size or the no. of elements in TreeMap. The syntax of size() method is:

Syntax of size() method:

Returns:
the number of elements in this map

Example:

Output:

 

Related posts

Leave a Comment