Solved

multiple null  as key to hashmap

Posted on 2011-09-16
6
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

627 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