File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Code to store null keys and null values  in 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 » Beginning Java
Bookmark "Code to store null keys and null values  in hashmap" Watch "Code to store null keys and null values  in hashmap" New topic
Author

Code to store null keys and null values in hashmap

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
can you give me hashmap code to store and retrieve null keys and null values in hashmap. I have searched but i could not find any.
I want to in depth in hashmap , so give me any url to read hashmap
lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
Posts: 130
Hi rajendra this link will give more details regarding HashMap
below code is for storing and retriving null values from HashMap

Map map=new HashMap();
map.put(null, null); //storing
System.out.println(map.get(null));//retrieving
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please CarefullyChooseOneForum. I'm moving this thread out of the servlets forum.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Rajendra Prakash wrote:I have searched but i could not find any.

How are you searching ? Where ? Searching "hashmap null keys" in Google will give you plenty of results.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

My Initial thought was that 'null' shouldn't be allowed as a Key. But it is and it should work like any other key.
which mean you should be able to do a put(null,null) and a get(null).

Whats the problem that you face when you tried it out?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
what i know about hashmap is , it allows one null as key and many null values . But the following code allows multiple null keys
HashMap <Integer,String>map = new HashMap<Integer,String>();
map.put( null,"null");
map.put(null,"Ian");
map.put( null,"Scott");
map.put(123, "Somebody else");
//map.put("raja","john");
String name = map.get(1);
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Rajendra Prakash wrote:But the following code allows multiple null keys

Not really. Only one null key is present in the map at once. Every time you add a new value for that key, you simply overwrite the old value - it's no longer present in the map. That's the same way a Map works for any key. For example:

The value of name, here, is "Scott". Both "null" and "Ian" were previously in the map, but they're not, anymore. They're gone.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Rajendra Prakash wrote:what i know about hashmap is , it allows one null as key and many null values . But the following code allows multiple null keys

Just print the content of the Map and see what it contains.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I understood this multple values for same key concept. But i read hashmap allows one null as key. But it accepts multple null keys
map.put(null,"rajl");
map.put(null,"Ian");
map.put(1,"Scott");
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Rajendra Prakash wrote:But i read hashmap allows one null as key. But it accepts multple null keys

Just because it accept doesn't mean it contains them all. I suggest you read the previous reply from Mike which explains what happens.
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
ok, is there any restriction for key should be string or Integer. like wise value should be string
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Rajendra Prakash wrote:But it accepts multple null keys

It accepts them only by forgetting about all previous values for that key. Only one value is stored in the map for a given key at a given time. Whenever a new value is stored for an already-existing key, the old value is removed, and the new one inserted in its place.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Rajendra Prakash wrote:ok, is there any restriction for key should be string or Integer. like wise value should be string

You declared that the map was a HashMap<Integer,String>, so yes. The key must be an Integer (or int, with autoboxing) and the value must be a String. Either can be null, but neither may be any non-null value that is not an Integer or String, respectively.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

If you want to do this then I think you will need to use org.apache.commons.collections.map.MultiValueMap from the apache commons collections project.
Here is some simple code to test it with:


As has been stated, the standard Map implementation will not deal with this that is why this specific class is necessary.

Sean

I love this place!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
For people using JDK 1.5 or later, I would prefer to recommend Google Collections, which has MultiMap. Same basic idea though.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Mike Simmons wrote:For people using JDK 1.5 or later, I would prefer to recommend Google Collections, which has MultiMap. Same basic idea though.


Thanks for adding that, I've not seen that before.

Sean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code to store null keys and null values in hashmap
 
Similar Threads
Help with Java Generics...
List in HashMap
HashMap size
null values in hash map
using item in arraylist as order