Solved

multiple null  as key to hashmap

Posted on 2011-09-16
6
437 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now