TreeMap size() method in java

Let’s learn TreeMap size() method in java.

TreeMap size() method in java

size() method of TreeMap class returns the number of key-value mappings in this map.

Syntax:

public int size()

Parameters:

size() method do not take any parameters.

Also read – TreeMap in java

Now let’s learn example on TreeMap size() method.

import java.util.TreeMap;
public class TreeMapSizeMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(32, "pineapple");
      tm.put(51, "watermelon");
      tm.put(38, "grapes");
      tm.put(69, "mango");
      tm.put(58, "apple");
      System.out.println("Given TreeMap is: " + tm);
      System.out.println("size of TreeMap is: " + tm.size());
   }
}


Output:

Given TreeMap is: {32=pineapple, 38=grapes, 51=watermelon, 58=apple, 69=mango}
size of TreeMap is: 5