1

I know about null key is not allowed in Hashtable because to store element in Hashtable hash code must required. But if key is null it will unable to calculate hash code for null key. But I don't understand but what is the exact reason in mind for Sun developers not to allow null value.

Someone says there is null check for value inside put method implementation and that's why it throws NullPointerException. But my question is why that null value check. Is there any specific reason behind it.

I went through lots of read but no got satisfied answer. Some one says there is ambiguity if there is null value and if you try to retrieve value using get() method it will return null and this null is because of actual value is null or key is missing that's why null and could not predict reason. So i need pin point answer with proof.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • 1
    Read the implementation of put method. Download the JDK and go through the code – nits.kk Aug 14 '18 at 12:56
  • I think your question is described in [this](https://stackoverflow.com/questions/11981852/why-hashtable-does-not-allow-null-keys-or-values) StackOverflow post very well – monsIgnore Aug 14 '18 at 13:07
  • I think you should ask SUN developers to know what the "exact reason in mind" was...and especially if you need proof! – user85421 Aug 14 '18 at 13:11

4 Answers4

2

You will get NULL for value if you do

hashtable.get("key")

and "key" is not in the map, then you don't need to store null values.

If you would be able to store null, you will never know what you had: null mapping or that is a missing mapping.

Simion
  • 353
  • 1
  • 9
  • Every downvote is appreciated with coment. – Simion Aug 14 '18 at 13:04
  • 1
    I didn't downvote, but also think that's not right answer because ```HashMap``` allows null for values – rechandler Aug 14 '18 at 13:06
  • 1
    It doesn't matter, you get value the same way from ```Hashtable``` and ```HashMap```, in one class it can be done, in the second you can't. So your argument is not correct. I agree that you don't need to store null values, but I isn't argument for NPE. – rechandler Aug 14 '18 at 13:10
  • Than maybe you can tell us what is an argument for OP? I suggested a reason why you don't need that. It has nothing to do with SUN developers. If someone will be able to explain me, why this is not an answer, than i will be gratefull. – Simion Aug 14 '18 at 13:13
  • 1
    @Simion you got a downvote (not from me) because you literally have not answered the question. Even with what you said, having potentially null keys and values is *still* a wrong decision. `Map.of` static methods in java-9 prove that – Eugene Aug 14 '18 at 13:14
  • @Eugene, rechandler, can you tell me why this is not an answer, you both say so, but not explain why ? – Simion Aug 14 '18 at 13:16
  • @Simion why did you remove the city you are from? such a pity... when you think we are from the same one ;) – Eugene Aug 14 '18 at 13:17
  • @Eugene yes, we are. But how can you answer to "what is the exact reason in mind for Sun developers not to allow null value" ?, are here a SUN(oracle) forum ? you can only explain why you would not want this ! I know that if someone is putting a question he has something in mind. And he searches help with the problem he faceof. – Simion Aug 14 '18 at 13:21
  • @Simion it's not a SUN forum - you're right. But if you watch multiple videos from Stuart Marks that created static factory methods like `Map.of` - he *does* say that null is seen as an design error in `HashMap` - thus my answer – Eugene Aug 14 '18 at 13:24
  • Your reasoning sounds plausible. I think if you begin your answer with something like "*I think Sun developers thought that...*", then it will be good. – rustyx Aug 14 '18 at 13:25
  • @ rustyx lol, of course i can write that, but what if Sun developers had completely other things in mind ? Unfortunate i'm not a medium. – Simion Aug 14 '18 at 13:30
  • Then why did you post your answer? Did you mean to comment instead? – rustyx Aug 14 '18 at 13:33
  • @rustyx, no, i put my answer to help a stranger, with some problems that he thinks he have during development. Sometimes you need another view to the problem you have, and it changes completely your ways of making code. – Simion Aug 14 '18 at 13:36
  • @Eugene it comes from Java9, but what to do will support of all that code written in Java5,6,7,8, i think majority of those projects will not be updated very soon, but they require support. This why still occurs a lot of questions on old code usage. – Simion Aug 14 '18 at 13:40
  • @Simion you are right, my point is different here. OP asks "But I don't understand but what is the exact reason in mind for Sun developers not to allow null value", my answer: "they allowed it, but now they think it was wrong, thus they close this gap" via `ConcurrentHashMap`, `static factory methods` in 9, etc – Eugene Aug 14 '18 at 14:01
  • @Eugene, i made an edit to the answer, added some more clarifications. Maybe this is more helpful now. – Simion Aug 14 '18 at 14:04
  • @Simion I will plus one this, if you also say that when you do `get` and it returns a `null` - you have no idea if that is a `null mapping` or that is a `missing mapping`, makes sense? – Eugene Aug 14 '18 at 14:05
  • @Simion sorry your verbiage is a bit confusing... I read it twice and did not understand that you actually said that – Eugene Aug 14 '18 at 14:08
  • @Eugene, Edited according to your suggestion. Tx. – Simion Aug 14 '18 at 14:12
  • @Simion well, now it makes a fairly decent answer ;) cheers! – Eugene Aug 14 '18 at 14:13
1

Hashtable is considered legacy code. You should use HashMap and it allow null for values and also one key can be null.

EDIT

After deeper search I may have argument for such decision. Hashtable is synchronized (and HashMap isn't).

From JavaDoc:

Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

As you can see successor of Hashtable is not HashMap as I previously write but ConcurrentHashMap. I was surprised that ConcurrentHashMap does not allows null. I start digging and found this:

From the author of ConcurrentHashMap himself (Doug Lea):

The main reason that nulls aren't allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can't be accommodated. The main one is that if map.get(key) returns null, you can't detect whether the key explicitly maps to null vs the key isn't mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.

So maybe authors of Hashtable have the same reason as authors of ConcurrentHashMap

rechandler
  • 756
  • 8
  • 22
0

From Java Documentation

To successfully store and retrieve objects from a hash table, the objects used as keys must implement the hashCode method and the equals method

Null is not an object, so can not call .equals() or .hashCode() on it, so the Hashtable can't compute a hash to use it as a key

Hashtable containsValue(Object value) function throw NullPointerException if the value is null so for the value also not allowed null

Indrakumara
  • 1,595
  • 17
  • 22
  • 1
    The question was about values, not keys. The question already mentions the facts you state here. – OhleC Aug 14 '18 at 13:04
0

Having a null value is still considered a bad decision in HashMap and the new Map classes and the static factory methods in java-9 prove that:

Map.of("test", null)

will throw a NulPointerException

Eugene
  • 117,005
  • 15
  • 201
  • 306