Solved

multiple null  as key to hashmap

Posted on 2011-09-16
6
440 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
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.

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java jdbc batch example 8 41
hibernate insert example 13 34
Java exception bubble up 2 21
ejb entity bean example issue 2 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

821 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