Can HashMap store null values?If yes, how many and in which one?

Showing Answers 1 - 30 of 30 Answers

Arun Kantilal Rajput

  • Nov 22nd, 2006
 

hi,

yes, HashMap can store null values.

u can have new HashMap("a",null);

also key can be null ie new HashMap(null,null);

value can be retrieved by get method ie get(null) returns null.

chow.

  Was this answer useful?  Yes

Avitra

  • Nov 23rd, 2006
 

hashmap accepts null values.

we can set null values by put method & get the values by get method

put(null,null)

  Was this answer useful?  Yes

Satish Bakde

  • Nov 24th, 2006
 

Yes, HashMap can store null values. The argumemnts to the put method of HashMap takes two arguments key-which is object and value-which is again object. null is empty unallocated object. So it can be used as key as well as value.

There can be mutiple null values present in a HashMap. Restriction is on keys which should be unique. HashMap allows only one null key. If we put more with key null, it replaces previous value with new value.

Andrews

  • Nov 30th, 2006
 

   Yes, A hashmap allows null values.But it allows only one Null Key,  because in Hash Maps keys maintains the unique Key property.

Thanks & Regards,

Andrews.

  Was this answer useful?  Yes

Mayank Shrivastava

  • Dec 11th, 2006
 

Yes Hashmap can store null values ,it can hold only one null key but it can hold more then one null values.

  Was this answer useful?  Yes

Kanhaiya

  • Dec 12th, 2006
 

HashMap can store null values. You can store null key and value as much u want.

  Was this answer useful?  Yes

santh kumar

  • Dec 13th, 2006
 

Hi Guys, It's a Solid Answer for all ur doubts. i gave u with an example. HashMap hs=new HashMap();1) hs.put("1", "Russel");2) hs.put("2","Kumble");3) hs.put("null","........." );4) hs.put("null","Stevewaugh " );5) hs.put(" "," " );6) hs.put("2","RanaTunga"); 7) System.out.println(hs.get("2"));8) System.out.println(hs.get("1"));9) System.out.println(hs.get(" "));10) System.out.println(hs.get("null")); It allows numerous null values.it allows duplicate keys also.for example key=2 in the above program it possess recent value is RanaTunga instead of Kumble.for line:5 it prints even space also, as i said earlier what would be the result for (10) ? guess it ?, It's ur Turn.

  Was this answer useful?  Yes

santh kumar

  • Dec 13th, 2006
 

I entered clearly. with spaces and next lines.Why i'm getting data collabarated? it is not easy to read. any suggestions would be appreciated. what should i do to be in the same format exactly how i entered. with regards, santh

  Was this answer useful?  Yes

vikas singh

  • Dec 23rd, 2006
 

hi

your ans is too good.........

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions