| Author |
Hashtable and Hashmap Doubt
|
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
I read in K&B Book that,
Hashmap allows one null key and multiple null values in a collection. wheras Hashtable dosent let you add anything that's null.
Why is this distinction as both are unsorted, unordered?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Hashtable is thread-safe Abhi, and it was there in java since 1.0, you can see its naming convention "t" in Hashtable is written in small. It was designed such that it will not allow any null key or null value.
But HashMap is added in java 1.2 version, so designer thought null should be allowed. Reason I do not know, any sheriff or knowledgeable guy could say the reason.
|
SCJP 6
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
Punit,
You just inserted MULTIPLE null keys into the hashMap...
why was there no exception?
|
===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
HashMap does not store duplicate values. So although I have inserted multiple key, it will store only one null key and set the latest value to it.
Run this code, and tell me what you learned ?
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
Duran Harris wrote:
You just inserted MULTIPLE null keys into the hashMap...
why was there no exception?
If the map previously contained the same key the old value is replaced.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
|
Oh ok...Got it
|
 |
 |
|
|
subject: Hashtable and Hashmap Doubt
|
|
|