79452946
The difference between HashMap and Hashtable
The difference between HashMap and Hashtable is a frequently encountered problem in the interview. The problem seems simple, but if you delve into it, you can get a lot of knowledge. In this paper, we compare and summarize the sources, characteristics, algorithms and other aspects of the two. Strive to multi-angle, all-round display of the difference between the two to achieve the end of this issue.
1 author
Hashtable's
HashMap's
The author of the Hash map is more famous than the author of the Hashtable, the concurrent great god Doug Lea. He wrote the Util.concurrent bag. There are concurrent programming Bibles concurrent programming in Java:design principles and Patterns book. His personal homepage: http://g.oswego.edu/
Josh Bloch has led the design and implementation of many Java platform features, including the Java Collection Framework, the Java.math package, and the assert mechanism. There is a book of effective Java.
Arthur Van Hoff was the first Sun Microsystems company in Silicon Valley to work on the early development of Java programming languages. Many aspects of JDK 1.0 have been designed and implemented, including Java compilers, Java debuggers, many standard Java classes, and HotJava browsers. A number of successful companies were subsequently created, including Marimba (1999 IPO), Strangeberry (acquired by TiVo), ZING (later acquired by Dell) and Ellerdale (later acquired by Flipboard). The Java naming source comes from a combination of developer names: James Gosling, Arthur Van Hoff, and Andy Bechtolsheim initials.
Neal Gafter is the main designer and creator of the Java SE 4 and 5 language enhancements, and his Java closure implementation has won the OPENJDK Innovator Challenge Award. He is also continuing to participate in the language development of SE 7 and 8. Neal worked for Google's online calendar, once a member of the C + + Standards Committee, and developed C and C + + compilers at Sun Microsystems, Microtec Research Institute and Texas Instruments leadership. Now Neal is developing the. NET Platform programming language at Microsoft. Neal is a partner of the Java Puzzlers:traps, pitfalls and Corner Cases (Addison wesley,2005) book. He holds a PhD in computer science from the University of Rochester.
These authors are the most famous people in Java and the entire IT world. Only these master characters can write hashmap such a path to Jane's data type.
2 generation Time
Hashtable is the data structure of the key-value mappings provided when Java was first published, and HashMap is generated in JDK1.2. Although Hashtable than HashMap appear earlier, but now Hashtable basically has been deprecated. And HashMap has become one of the most widely used data types. The reason for this is that Hashtable is thread-safe and less efficient. On the other hand it may be because Hashtable did not follow the hump naming law ...
3 inherited parent classes are different
HashMap and Hashtable not only the author, but also the parent class is different. HashMap is inherited from the Abstractmap class, and Hashtable is inherited from the dictionary class. But they all implement the three interfaces of map, cloneable (replicable), Serializable (serializable) at the same time.
The dictionary class is a class that has been deprecated (see comments in its source code). The parent class is discarded, and no one naturally hashtable it with its subclasses.
* Note:this class is obsolete. New implementations should
* Implement the Map interface, rather than extending this class.
4 different interfaces available externally
Hashtable provides two methods for Elments () and contains () more than HashMap.
The Elments () method inherits from the parent class dictionnary of Hashtable. The elements () method is used to return an enumeration of the value in this hashtable.
The contains () method determines whether the hashtable contains the incoming value. Its role is consistent with Containsvalue (). In fact, Contansvalue () simply invokes the contains () method.
5 support for NULL key and null value is different
Hashtable neither supports null key nor does IT support null value. Hashtable is described in the comments for the put () method.
When key is null, call the Put () method, and run to the following step to throw a null pointer exception. Because it takes a null value to invoke the method.
When value is a null value, Hashtable restricts it, and running to the following step also throws a null pointer exception.
In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, the key may not be in HashMap, or the value corresponding to the key may be null. Therefore, the get () method cannot be used in HashMap to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.
6 Thread security is different
Hashtable is thread-safe, and the Synchronize method is added to each of its methods. In the multi-threaded concurrency environment, you can use Hashtable directly, do not need to implement its own method to achieve synchronization
HashMap is not thread-safe, and in the context of multi-threaded concurrency, it is possible to create deadlocks and other problems. The specific reasons are analyzed in detail in the next article. When using HashMap, you have to increase your own synchronization,
Although HashMap is not thread-safe, it is much more efficient than Hashtable. This is a reasonable design. In our daily use, most of the time is single-threaded operation. HashMap This part of the operation free. Thread-safe Concurrenthashmap can be used when multi-threaded operations are required. Although Concurrenthashmap is also thread-safe, it is much more efficient than Hashtable. Because Concurrenthashmap uses a segmented lock, the entire data is not locked.
7 times the internal implementation of the Calendar method is different
Hashtable and HashMap all use the Iterator. For historical reasons, Hashtable also used the enumeration approach.
The HashMap iterator is a fail-fast iterator. When there are other threads that change the structure of the HashMap (add, delete, modify elements), the concurrentmodificationexception will be thrown. However, removing an element through the iterator remove () method does not throw a concurrentmodificationexception exception. But this is not a certain behavior, depends on the JVM.
In previous versions of JDK8, Hashtable did not have a fast-fail mechanism. In JDK8 and later versions, Hashtable is also used Fast-fail, the source code is as follows:
The use of Modcount is similar to the CAS (Compare and Swap) technology in concurrent programming. We can see this method, each time in the occurrence of additions and deletions will appear modcount++ action. And Modcount can be understood to be the state of the current Hashtable. Each time an operation occurs, the state goes one step forward. Setting this state is mainly due to the fact that the container classes such as Hashtable are used when iterating to determine whether the data is obsolete. Although the Hashtable uses a native sync lock to protect data security. However, when iterative data is present, there is no guarantee that edge iterations will work correctly. The value is then used to mark the state. Once the state has changed during the iteration, an exception is thrown quickly, terminating the iteration behavior.
8 The initial capacity size differs from the size of each expansion capacity
Hashtable default initial size is 11, then each expansion, the capacity becomes the original 2n+1. HashMap The default initialization size is 16. After each expansion, the capacity becomes twice times the original.
When created, if a capacity initial value is given, Hashtable will directly use the size you given, and HashMap will expand it to a power of 2. That is to say Hashtable will try to use prime, odd. HashMap always uses the power of 2 as the size of the hash table.
The reason for this difference is that the focus of Hashtable and HashMap design is different. The focus of Hashtable is that the results of the hashes are more uniform, resulting in reduced hash collisions. When the hash table size is prime, the result of a simple modulo hash is more uniform. The HashMap is more concerned with the computational efficiency of hash. In the modulus calculation, if the modulus is a power of 2, then we can directly use the bitwise operation to get the result, the efficiency is much higher than doing division. HashMap in order to speed up the hash, the size of the hash table is fixed to a power of 2. Of course, this introduces the problem of uneven hash distribution, so hashmap to solve this problem, and the hash algorithm has made some changes. This results in different methods for calculating hash values for Hashtable and HashMap.
9 methods for calculating hash values are different
In order to get the position of the element, we first need to calculate a hash value based on the key of the element, and then use the hash value to calculate the final position.
Hashtable directly uses the object's hashcode. A hashcode is a value of the type int that the JDK calculates based on the address of the object or a string or number. The final position is then used in addition to leaving the remainder.
Hashtable a division is required when calculating the position of an element, and the division operation is time-consuming.
HashMap in order to improve the computational efficiency, the size of the hash table is fixed to a power of 2, so in the model budget, do not need to do division, only need to do bit operation. Bit operations are much more efficient than division.
Although the efficiency of HashMap increased, but the hash conflict has also increased. Because it is the same probability that the hash value is the same as the lower one, and the computed bit operation
In order to solve this problem, HashMap calculates the hash value according to the hashcode, and then makes some operations on the hash value to scatter the data. The resulting position is more dispersed, thus reducing the hash conflict. Of course, to be efficient, hashmap only did some simple bit processing. So as not to offset the efficiency gains from using the power of 2.
Attach a note to this question:
Joshua bloch:the downside of using a power-of-two is, the resulting hash table is very sensitive to the quality of th E 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 it is true and we put in a secondary (or "defensive") hash function if we switched to T He power-of-two hash table. This hash function was applied to the results of hashcode before masking off, the low order bits. Its job was to scatter the information-all-the-bits, and in particular, into the-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.4 turned off to be insufficient. We knew that is a theoretical possibility, but we thought the 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) have strong statistical properties that pretty much guarant EE good bucket distribution.
-------------------
Comparison of HashMap and Hashtable