In this tutorial we willl learn how to get the size of a TreeMap. We are using size() method of TreeMap class to get the number of key-value mappings of a TreeMap. Here is the complete code:
Example
import java.util.TreeMap; class TreeMapSize { public static void main(String args[]) { // Create a TreeMap TreeMap<String, String> treemap = new TreeMap<String, String>(); // Put elements to the map treemap.put("Key1", "Value1"); treemap.put("Key2", "Value2"); treemap.put("Key3", "Value3"); treemap.put("Key4", "Value4"); treemap.put("Key5", "Value5"); // Displaying size of the TreeMap /* public int size(): Returns the number of key-value * mappings in this map. */ System.out.println("Size of TreeMap: "+treemap.size()); } }
Output:
Size of TreeMap: 5
Leave a Reply