multiple null  as key to hashmap

Posted on 2011-09-16
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
Question by:gudii9
  • 5
LVL 47

Expert Comment

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

Accepted Solution

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
LVL 47

Expert Comment

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");

        String sss = m.get(null);


Open in new window



Open in new window

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

ID: 36552848

see here:

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.)
LVL 47

Expert Comment

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(

Open in new window


Assisted Solution

reijnemans earned 150 total points
ID: 36558337

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?




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