Solved

multiple null  as key to hashmap

Posted on 2011-09-16
6
439 Views
Last Modified: 2012-05-12
can we have multiple null  as key to hashmap. i was not clear on this concept. please advise.thanks  in advance
0
Comment
Question by:gudii9
  • 5
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36552834
null can be a key,
but second time as any other key with HashMap it will overwrite thae Entry
0
 
LVL 47

Accepted Solution

by:
for_yan earned 350 total points
ID: 36552836
So you cannot have multiple nulls as a key, as well as any other key - it should be unique
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36552843
As you see "ab" is overwritten by "cd" :

        HashMap<String,String> m = new HashMap<String,String>();
        m.put(null, "ab");

         m.put(null, "cd");
        
          m.put("a","nm");

        String sss = m.get(null);

        System.out.println(sss);
        

Open in new window


Output:

cd

Open in new window


0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 47

Expert Comment

by:for_yan
ID: 36552848



see here:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html

This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36552855

As opposed to HashMap,
Hashtable does not allow null as the key  and throws NullPointer exception

       Hashtable<String,String> t = new Hashtable<String,String>();

         t.put(null, "ef");

Open in new window



Exception in thread "main" java.lang.NullPointerException
	at java.util.Hashtable.put(Hashtable.java:399)

Open in new window

0
 
LVL 4

Assisted Solution

by:reijnemans
reijnemans earned 150 total points
ID: 36558337
Hi

As described by for_jan it's not possible to add multiple null key-values into a hashmap. But why do you want to put multiple null values as key in a hashmap? Maybe we could figure out a solution for that?

Regards,

reijnemans


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 36
hibernate jars 4 30
iterator/ListIterator approach 17 26
Unhandled exception type Exception 18 31
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question