1.

Does Hashmap Allow Null Keys And Null Values?

Answer»

HashMap allows one null key and any number of null values. If another null key is added it won't cause any ERROR. But the value with the new null key will override the value with OLD null key.

As exp. If you have a Map, cityMap and you ADD TWO values with null keys in the cityMap, Kolkata will override the New Delhi as only one null key is allowed.

cityMap.put(null, "New Delhi");
cityMap.put(null, "Kolkata");

HashMap allows one null key and any number of null values. If another null key is added it won't cause any error. But the value with the new null key will override the value with old null key.

As exp. If you have a Map, cityMap and you add two values with null keys in the cityMap, Kolkata will override the New Delhi as only one null key is allowed.

cityMap.put(null, "New Delhi");
cityMap.put(null, "Kolkata");



Discussion

No Comment Found