How to Get Size And Value of a TreeMap in Java


In this example we will show how to get the size or nubmer of key value pairs stored in TreeMap with size method.

Source Code

package com.beginner.examples;

import java.util.TreeMap;

public class GetSizeOfTreeMap {

	public static void main(String[] args) {
		
		//create TreeMap.
		TreeMap treeMap = new TreeMap();
		
		treeMap.put("1","Alice");
		treeMap.put("2","Bob");
		treeMap.put("3","Jack");
		
		/*
		 * To get the size of TreeMap use size method of TreeMap.
		 */
		System.out.println("The size of TreeMap is : " + treeMap.size());

	}

}

Output:

The size of TreeMap is : 3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments