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 Java in General and the fly likes Why Null value is allowed in ArrayList,Vector,Set or HashMap? 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 » Java in General
Bookmark "Why Null value is allowed in ArrayList,Vector,Set or HashMap?" Watch "Why Null value is allowed in ArrayList,Vector,Set or HashMap?" New topic
Author

Why Null value is allowed in ArrayList,Vector,Set or HashMap?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi All,

Why null values are allowed in Set,Vector ,List.

and one Null key is allowed in HashMap.

Regards,
Prabhat
Alvin Watkins
Ranch Hand

Joined: May 25, 2011
Posts: 53
Because I may need null values in a Collection. For instance, I may have static positions (say seats around a poker table) and I need to sometimes have values matching those keys and other times I need to have null values there (for instance no player is seated in the poker table's seat at static position 'n' so don't deal to that position).

There are many times that being able to put null values into Collections is very useful.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes that's true.

means if we want to check if the list has null value or something list that in our real world object. We need to use the Null object.

and why HashTable doesn't allow null key while HashMap allows?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Prabhat Ranjan wrote:and why HashTable doesn't allow null key while HashMap allows?

Dunno. Probably something to do with how it works internally.

And strictly speaking, the restriction is not particularly important. In the case of Alvin's Poker game, it would be perfectly easy (and probably clearer) to create a Player that signifies an empty seat, viz:
public static final Player EMPTY_SEAT = new Player("Not-A-Player");

Personally, I try to avoid using nulls whenever possible.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Prabhat Ranjan wrote: . . . We need to use the Null object. . . .
What’s a Null object? Do you mean the null literal? That points to no object at all, so there is no such thing as a null object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Null value is allowed in ArrayList,Vector,Set or HashMap?
 
Similar Threads
new SCJP in town :)
How to access Null key value of an HashMap
Please Help: Putting Vector objects into HashMap
Regarding HashMap & Hashtable
Memorizing the Collections