Now, for a regular HashMap, it is assumed, that it is going to get called by a single thread, hence there is no possibility of key getting removed in the middle of "contains" check and "get". So HashMap can tolerate null values. However for Hashtable and ConcurrentHashMap, the expectations are clear that multiple threads are going to act on the data. Hence they cannot afford to allow null values and give out incorrect answer. Same logic goes for keys.
That is correct, it can happen. 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? Whereas null as a key, i think can be used to define some default data.
Usually null as a key does not make much sense. Sir HashMap is also internally uses hashCode method for inserting an element in HashMap, so I think this will be not the proper reason for "why HashTable allow null key". Making the map able to handle null keys and values will eliminate the need for null checking before calling the api. So the map api create less "surprises" during runtime.
For example, it is common that map will be used to categorize a collection of homogeneous objects based a single field. When map is compatible with null, the code will be more concise as it is just a simple loop without any if statement of course you will need to make sure the collection does not have null elements.
It is not practical to rely on the map to reject nulls - that means exception will be thrown and you have to catch and handle it. Or, to get rid of the exception you will have to ensure nothing is null before calling the map methods - in which case you don't care if map accepts null since you filtered the input anyway.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why is it useful to have null values or null keys in hash maps?
Ask Question. Asked 11 years, 2 months ago. Active 3 years, 8 months ago. Viewed 54k times. Questions: Why is this so? How is it useful to have such a key and values in HashMap? Improve this question. Community Bot 1 1 1 silver badge. What do you mean by the "importance"? Michael Mrozek: Probably significance. Add a comment. Active Oldest Votes. Why is this so? HashMap is newer than Hashtable and fixes some of its limitations.
I can only guess what the designers were thinking, but here are my guesses: Hashtable calculates a hash for each key by calling hashCode on each key. This would fail if the key were null, so this could be a reason for disallowing nulls as keys. The method Hashtable. If null were a valid value it would be ambiguous as to whether null meant that the key was present but had value null, or if the key was absent.
Difference between Collections. What are the different Reference types available in Java? Which methods you need to override to use an object as key in HashMap? How Remove method works internally in Java HashMap? Explain NavigableMap in Java collection. Explain rehashing process in Java HashMap. How null key is handled in HashMap? What is the data-structure behind the Java HashMap implementation? Why are immutable objects in HashMap so effective? Why null is not allowed in ConcurrentHashmap?
Does ConcurrentHashMap allow null keys and null values? Difference between remove and clear methods in HashMap. Is hashcode method invoked internally when calling get and put method in Java? How hash collision is handled in HashMap?
What are the interfaces that TreeMap implements? What interfaces that HashMap implements? Big O notation for TreeMap in Java. Is TreeMap synchronized in Java? Does TreeMap allow null key? Key object constraints on a TreeMap.
Does IdentityHashMap use hashcode method? Does EnumMap allow null key?
0コメント