This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why HashMap contains null key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why HashMap contains null key" Watch "Why HashMap contains null key" New topic
Author

Why HashMap contains null key

Sushil Pathak
Greenhorn

Joined: May 03, 2013
Posts: 1
Hi,
please provide me why HasMap contains one null key and how it can be managed please provide solution with example.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Sushil Pathak wrote:please provide me why HasMap contains one null key and how it can be managed please provide solution with example.

First: Welcome to JavaRanch.

Second: In answer to your question - No. That's not how this site works; we expect you to do your own work here.

What I will tell you is that a HashMap (indeed, ANY Map) can only contain one of any key, be it null or anything else. Some Map implementations choose not to allow nulls, and if they don't, there's probably a good reason; but don't expect the designer to tell you. That's what Object-Orientation is all about: hiding information.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Read this thread for a better perspective on this aspect of a HashMap. There are many ways to look at a glass of water. One might say it is half full, another might see it as half empty. It is a design issue that prompted the author of this class to take this step. Joshua would be able to answer your query perfectly.


~ Mansukh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why HashMap contains null key
 
Similar Threads
Hashtable doesn't allow null value and HashMap allows only one null as key
HashMap
Why HashMap allows to add one NULL key ?
Collections
sort HashMap by Key object's value