What is the advantage of multiple null keys in hashmap?

Just for experimentation, I added a few nullkeys to the instance Hashmap. And he did not complain. What is the use of this?

The code,

Map hmap = new HashMap();
hmap.put("sushil","sushil11" );
hmap.put(null,null);
hmap.put(null,"king");
hmap.put(null,"nagasaki");
hmap.put(null,null);

How many keys are on the card?

+5
source share
5 answers

It is used to get the switch: case: default key.

Example:

Definition of the problem: a cafe in the CS building. They provide coffee for CS Student for $ 1.00, IT students for $ 1.25 and others for $ 1.50.

Then Map will:

Key → Value

IT → 1.25

CS → 1.00

null → 1.50

if (map.containsKey (department))

price = map.get (dept);

still

price = map.get (null);

PS - I’m not a "Department" if that word. :)

+3

, null -keys. null .

+10

hashmap , . ( MultiMap )

+7

API :

size: .

hmap.size();

, / .

+4

All Articles