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?
Who is Participating?

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

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.

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

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:

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


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.
OWASP: Forgery and Phishing

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

Why should you assign a value of null?

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

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.
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.

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

From novice to tech pro — start learning today.