A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Can HashMap store null values?

can we store null values in hashmap or in hashmap can i store multiple null values?
can you store multiple null values in hashmap ?

Yes, we can Store null values in HashMap using only one null key. If we store multiple values using null key, then the latest value on key will be displayed.

import java.util.HashMap;

public class TestHashMap
    public static void main(String a[])
         HashMap hm = new HashMap();
         hm.put(1, "value1");
         hm.put(2, "value2");
         hm.put(null, "heera");
         hm.put(null, "heera babu");

Console Output:

{null=heera babu, 1=value1, 2=value2}