How to get size of HashMap

In order to get size of HashMap , we will use size() method. Below we will write code for how to get size of Hashmap using get() method example .

import java.util.HashMap;
import java.util.Map;

public class HashMapKeyCount {

public static void main(String[] args) {

Map<Integer,String> hm = new HashMap<Integer,String>();

* Associates the specified value with the specified key in
this map (optional operation). If the map previously
contained a mapping for the key, the old value is
replaced by the specified value
hm.put(2, "amit");
hm.put(5, "yogesh");

//Returns the number of key-value mappings in this map

System.out.println("HashMap size = "+hm.size());




HashMap size = 5


Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 4 =