But since Hashtables and ConcurrentHashMaps do not allow null keys and values, it is not necessary for them to implement contains and get check in the first place. They can directly get the value because they know that if the get method returns null, the only reason for that is the key is not present and not because the value could be null.
The contains and get check is necessary only for HashMaps because they allow the null values and thus need to resolve the ambiguity about whether the key is not found or the value is null. If you want a drop-in replacement that doesn't require anything else than changing the class and works in every scenario, there is none. The most similar option would be ConcurrentHashMap which is thread safe but doesn't support locking the whole table :. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details.
HashMap is a better replacement for single threaded applications or any time synchronization is not a requirement, because of the performance impact synchronization introduces. Python Javascript Linux Cheat sheet Contact. Why Hashtable does not allow null keys or values? Edit From Hashtable JavaDoc: To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and the equals method.
Let's take a look at below code: if map. The reason is the reason on the accepted answer: Hashtable is old. HashMap is not. Neither Hashtable nor ConcurrentHashMap support null keys or values. HashMap does. Joshua Bloch : The downside of using a power-of-two is that the resulting hash table is very sensitive to the quality of the hash function hashCode. It is imperative that any change in the input must affect the low order bits of the hash value.
Ideally, it should affect all bits of the hash value with equal likelihood. Because we have no assurance that this is true, we put in a secondary or "defensive" hash function when we switched to the power-of-two hash table. This hash function is applied to the results of hashCode before masking off the low order bits.
Its job is to scatter the information over all the bits, and in particular, into the low order bits. Of course it has to run very fast, or you lose the benefit of switching to the power-of-two-sized table. The original secondary hash function in 1.
We knew that this was a theoretical possibility, but we thought that it didn't affect any practical data sets. We were wrong. The replacement secondary hash function which I developed with the aid of a computer has strong statistical properties that pretty much guarantee good bucket distribution.
Instead, you can use the boxed Boolean class which is an ordinary class rather a primitive type , or a three-valued enum. Why Hashtable does not allow null keys or values?
Asked 5 Months ago Answers: 5 Viewed 37 times. From Hashtable JavaDoc :. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and the equals method.
Since null isn't an object, you can't call. Login using GitHub Register. Ask a Question. Please log in or register to add a comment. Please log in or register to answer this question. Edit From Hashtable JavaDoc : To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and the equals method. Related questions. Why is Powershell Hashtable Count and Keys Properties getting overloaded.
0コメント