• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code to store null keys and null values in hashmap  RSS feed

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please CarefullyChooseOneForum. I'm moving this thread out of the servlets forum.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4018
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4018
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, is there any restriction for key should be string or Integer. like wise value should be string
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
machines help you to do more, but experience less. Experience this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!