• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map null values doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
� Stores key/value pairs.
� Allows null elements, keys, and values.
� Duplicate entries replace old entries.
� Entries are not sorted using a Comparator or the Comparable interface.
� The iteration order is unspecified.

Which of these classes provides the specified features?

a. LinkedList
b. LinkedHashMap
c. LinkedHashSet
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Answer f. I don't have doubt regarding the answer.

They have given the explanation about allowing null elements that is

1) The requirement to allow null elements is not satisfied by a Hashtable.So Hashtable won�t allow null elements.

But the coding
lhs.put("null", new Integer(7));
lhs.put("six", new Integer(6));

displays

null 7
six 6

2) The iteration order of LinkedHashMap and LinkedHashSet is not unspecified.What does this mean?

I know that LinkedHashMap and LinkedHashSet are ordered like they were added....


Please help to clarify those doubts.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) "null" is not null. It is a String literal containing those characters.

(2) The original question asked which of those collections had orders that were unspecified. Because LinkedHashMap and LinkedHashSet do specify the order in which elements will be iterated, they don't satify the criterion.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith- I have got the second answer that LinkedHashMap and LinkedHashSet that gives the iteration order like they were inserted. So the iteration order is not unspecified. When we look at TreeMap and TreeSet, their iteration order is not unspecified as they sort elements. When comes to Hashtable,the elements order is unspecified.Am I correct?

Regarding my first doubt,

Hashtable lhs = new Hashtable();
String p="null";
lhs.put("p", new Integer(p));

Giving runtime error.Why?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:

Hashtable lhs = new Hashtable();
String p="null";
lhs.put("p", new Integer(p));

Giving runtime error.Why?



In this code, you are trying to create a Integer object from string "null" which will result in NumberFormatException
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


� Allows null elements, keys, and values.




Can anyone please give me an example source code for this
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you add null as a key in Map, what it does is it mask the null to NULL_KEY (an instance of Object)

static final Object NULL_KEY = new Object();

So basically in case of null, the key which gets stored is an instance of Object not null.

Naseem
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above code will throw NullPointerException if HashMap is replaced with HashTable
[ August 08, 2006: Message edited by: Kishore Balla ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But,I didn�t get NullPointerException when I run the same coding with Hashtable.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But,I didn�t get NullPointerException when I run the same coding with Hashtable.



Surely you won't get any NullPointerException with Hashtable as key is not null.

Try this:

new HashMap().put(null, ""); // compiles and run fine
new Hashtable().put(null, ""); // throws Exception at runtime
new Hashtable().put("", null); // throws exception at runtime.

If key or value is null in Hashtable, then put() method will throw NullPointerException.

Naseem
[ August 07, 2006: Message edited by: Naseem Khan ]
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:
But,I didn�t get NullPointerException when I run the same coding with Hashtable.




Are you sure? This is what I got when I ran the code with Hashtable

C:\Documents and Settings\kishore\Desktop\SCJP>java Test
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at Test.main(Test.java:9)


CODE:

[ August 07, 2006: Message edited by: Kishore Balla ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you sure?...



That wasn't the same code as you posted the first time.
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:


That wasn't the same code as you posted the first time.



The only change is previous previous code has HashMap.. (I've told him that replacing HashMap with Hashtable will result in exception)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first code you wrote
String p= "test";

In the second
String p= null;
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
In the first code you wrote
String p= "test";

In the second
String p= null;



My Mistake.. I was testing it with string and null values.. Forgot to change it back to null before copy-pasting
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic