why HashTable don't allow null as key and value

why HashTable don't allow null as key and value? and other collection class allows null value?
Asheesh_KumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n_sachin1Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
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
Kevin CrossChief Technology OfficerCommented:
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

sciuriwareCommented:
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
Kevin CrossChief Technology OfficerCommented:
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
sciuriwareCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.