Improve Article. Like Article. Previous Hashtable contains Method in Java. Next Hashtable in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
Writing code in comment? Some groups of implementations are similar in the operations they perform, and it is common for novice programmers to use them interchangeably.
ArrayList and LinkedList are two such implementations that are often confused and incorrectly applied in applications. I have written a post on ArrayList vs LinkedList that explains the nuances of them. They are HashMap and Hashtable. A Map stores key-value pairs where duplicate keys are not allowed. HashMap extends the AbstractMap class and implements the Map interface. On the other hand, Hashtable inherits the Dictionary class and also implements the Map interface.
As both Hashtable and HashMap implements Map , they are similar as both stores key-value pairs where the keys are unique and stored as hash values. To store an element in a Hashtable or HashMap , you need to specify a key object and its associated value.
The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. The primary difference between HashMap and Hashtable is that HashMap is not thread-safe, and therefore cannot be shared between multiple threads without external synchronization. On the other hand, Hashtable is thread safe, and therefore can be shared between multiple threads.
In addition to these differences, one commonly asked question is Why HashMap stores one null key but Hashtable does not? HashMap , allows storing one null key and multiple null values.
The reason for allowing only one null key is because keys in a HashMap has to be unique. On the other hand Hashtable does not allow null keys. This is because the objects used as keys in a Hashtable implements the hashCode and equals methods for their storage and retrieval. Since null is not an object it cannot implement the methods. If you try hashing a null key, it will throw a NullPointerException. Let us perform some operations on HashMap and Hashtable.
The class overrides the hashCode method. The hash code implementation generates unique hash code for each object based on their state. This means if you have objects with the same state, you will get the same hash code. To start Ubuntu i How to Hide the contact form from home page and sidebar. We can not hide the the recently launched Blogger Contact form permanently if we do so, we can cant further use custom contact form on Autoboxing and Unboxing in Java.
Autoboxing and Unboxing in Java Hi guys! Welcome you again to brajeshNotes. In today's session , we will talk about what is auto boxing How to Download and install Tomcat server on linux mint OS. How To Install java 8 On Ubuntu Perhaps they saw the need for a null key, and more importantly - null values, and added it in the HashMap implementation. HashMap is newer, and has more advanced capabilities, which are basically just an improvement on the Hashtable functionality.
When HashMap was created, it was specifically designed to handle null values as keys and handles them as a special case. 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.
The main reason why Hashtable and ConcurrentHashMap do not allow null keys or values is because of the expectation that they are going to be used in a multi-threaded environment. For a minute, let's assume that null values are allowed. In this case the hashtable's "get" method has ambiguous behavior. It can return null if the key is not found in the map or it can return null if the key is found and its value is null.
When a code expects null values, it usually checks if the key is present in the map so that it can know whether the key is not present or the key is present but value is null. Now this code breaks in a multi-threaded environment.
0コメント