Returns the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.
Program
package com.candidjava;
import java.util.Map;
import java.util.WeakHashMap;
/*
* @author : Mohan
* @description : The size() method is used to return the number of key-value mappings in this map.
*/
public class WeakHashMapSize {
public static void main(String[] args) {
Map<String, String> weakHashMap = new WeakHashMap<String, String>();
weakHashMap.put("1", "anandh");
weakHashMap.put("2", "hari");
weakHashMap.put("3", "vinoth");
weakHashMap.put("4", "kamal");
weakHashMap.put("5", "karthic");
weakHashMap.put("6", "mohan");
System.out.println("Map size: " + weakHashMap.size());
}
}
Output
Map size: 6
Explanation
public int size()
Returns the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.