To get size of HashMap we use the int size() method provided by Map API.
The size method returns the number of key/value pairs that are available in that Map.
Example : Get Size of HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kscodes.sampleproject; import java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<String, String>(); System.out.println("Hashmap size is :: " + hashMap.size()); hashMap.put("USD", "American Dollar"); hashMap.put("AUD", "Australian Dollar"); hashMap.put("EUR", "Euro"); hashMap.put("JPY", "Japan Yen"); System.out.println("After adding some elements Hashmap size is :: " + hashMap.size()); } } |
Output
1 2 |
Hashmap size is :: 0 After adding some elements Hashmap size is :: 4 |