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?
HashMap <Integer,String>map = new HashMap<Integer,String>();
map.put(123, "Somebody else");
String name = map.get(1);
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.
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.
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.
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.