aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hash map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hash map" Watch "hash map" New topic
Author

hash map

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
it SCJP book by K Siera & Berts Bates it is written that "hash map can have one null key & multiple null values" null values is alright but how can it has null key. then can values be retrieved from it.

please explain
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

sweety sinha wrote: "hash map can have one null key & multiple null values" null values is alright but how can it has null key. then can values be retrieved from it.

Had you tried to put a "null" value as a key in HashMap instance, lets try it and tell us whether you're able to fetch the respective "value" from it ?



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

HashMap object hold key and it's corresponding value.

See the following non-generic code example



Ouput is English not Maths.
The second put method call changes value to English. Initially null key point to Maths but now point to English.
In such a way HashMap have unique keys and multiple values. Multiple values may be same or different but keys must have unique values.

I hope this clears


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
thanks guys for quick response
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
And watch out for TreeMap and SortedMap, which NOT ALLOWS for null keys (NullPointerException will arise at runtime).

Greetings.


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Just to point that a TreeMap can have a null key, as long as the TreeMap is constructed using a Comparator which handles keys with the null value.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hash map