得到HashMap的长度

import java.util.HashMap;

public class GetSizeOfHashMap {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    HashMap hMap = new HashMap();
        System.out.println("Size of HashMap : " + hMap.size());
       
        //add key value pairs to HashMap using put method
        hMap.put("1","One");
        hMap.put("2","Two");
        hMap.put("3","Three");
        System.out.println("Size of HashMap after addition : " + hMap.size());
       
        //remove one element from HashMap using remove method
        Object obj = hMap.remove("2");
        System.out.println("Size of HashMap after removal : " + hMap.size());
}

}
Console:
Size of HashMap : 0
Size of HashMap after addition : 3
Size of HashMap after removal : 2

你可能感兴趣的:(得到HashMap的长度)