?
Solved

why HashTable don't allow null as key and value

Posted on 2008-11-05
7
Medium Priority
?
1,390 Views
Last Modified: 2010-05-18
why HashTable don't allow null as key and value? and other collection class allows null value?
0
Comment
Question by:Asheesh_Kumar
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
n_sachin1 earned 336 total points
ID: 22884771
Another example of a collection class that doesn't allow null is java.util.Vector.
These two classes were available in jdk1.0 and the implementors just chose to put this restriction, as they needed to synchronize access to the entries.
The internal implementation has changed (to retrofit the later developments like the java.util.Map interface), but these classes behave the same (as JDK1.0 to maintain the external contract).

You could look at the jdk sources available in src.zip for jdk1.0 and compare that with the jdk that you are using today. You'd see the difference yourself
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 332 total points
ID: 22884976
Hello Asheesh_Kumar,

If I am understanding you correctly, you are looking for a collection you can use that will not reject an object if it is null.  I don't believe List will.

Here are some references on the differences in the Java Collections API interfaces/implementations:
http://java.sun.com/docs/books/tutorial/collections/implementations/index.html
http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html

Each has a purpose as you will see, so just align the collection with your code need and should work out fine. ;)

Regards,

mwvisa1
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22884995
If you need specific functionality like a HashTable, you can look at HashMap in same grouping as it also implements the Map interface but allows null values.
http://java.sun.com/javase/6/docs/api/java/util/HashMap.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 332 total points
ID: 22885432
Why should you assign a value of null?

How could you tell if null returned on a .get means "not found" or "null found"?

;JOOP!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22886216
Sometimes you are storing objects that need to have a null value.  I would also not recommend using for null key.  That eliminates the issue sciuriware presents as you know you have a found object by key you were searching on and it is just null at the moment.

Since the premise is that keys should unique, the HashMap will allow a null key but only once.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 23203941
This question started with "Why".
'n_sachin1' gave some more info,
but 'mwvisa1' and me ('sciuriware') gave the "because" of SUN's implementation:

: the possible ambiguity in the collection's methods return value.

;JOOP!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

850 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