Java Basics Tutorial: Comparison of Hashtable and HashMap

Source: Internet
Author: User

Java Basics Tutorial: Hashtable vs. HashMap 1. Some of the arguments about HashMap:

A) HashMap is actually a "chain-table hash" of the data structure, that is, the combination of arrays and linked lists. the underlying structure of the HASHMAP is an array, and each item in the array is a linked list .

b) Examples of hashmap have two parameters that affect their performance: "Initial capacity" and filling factor .

c) HashMap Implementation of the different steps, thread is not secure. Hashtable Thread Safety

d) Key-value in HashMap are stored in entry .

e) HashMap can deposit null keys and null values, does not guarantee that the order of elements is constant, its underlying use of arrays and linked lists, through the Hashcode () method and the Equals method to ensure the uniqueness of the key

f) There are three main ways to resolve conflicts: Addressing method, Zipper method, and re-hashing method. HashMap is the use of zipper method to solve the conflict of the hash.

Note: The linked list method is the same hash value of the object into a linked list in the hash value corresponding to the slot; the approach to conflict resolution with open addressing is to use some sort of probing (also known as probing) technique to form a probing (test) sequence in a hash table when a conflict occurs.   Finds the specified keyword along this sequence, either until a given key is found, or when an open address (that is, the address cell is empty) (to insert, in the case of an open address, the new node to be inserted is stored in the Address cell). Zipper method The conflict resolution approach is to link all keywords as synonyms to the same single linked list. If the hash list length selected is M, the hash list can be defined as an array of pointers consisting of M head pointers T[0..m-1]. All nodes with hash address I are inserted into a single linked list with T[i] as the head pointer. The initial value of each component in T should be a null pointer. In the Zipper method, the filling factor α can be greater than 1, but generally take α≤1. The Zipper method fits the size of the unspecified element.

2. The difference between Hashtable and HashMap:

A) different inheritance. public class Hashtable extends Dictionary implements maps public class HashMap extends abstractmap imple ments Map

b) The methods in Hashtable are synchronous, while the methods in HashMap are not synchronized by default. In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process .

c) in Hashtable, both key and value do not allow null values . 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, it can indicate that the key is not in the HASHMAP, or that the value corresponding to the key is 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.

D) The internal implementation of the two traversal modes is different. Hashtable and HashMap all use the iterator. For historical reasons, Hashtable also used the enumeration approach .

e) The hash value is used differently,Hashtable directly uses the object's hashcode. The hash value is recalculated by the HashMap.

f) Hashtable and hashmap their two internal implementations in the form of an array of initial size and expansion. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.

Note:      HashSet subclasses rely on the hashcode () and equal () methods to distinguish between repeating elements. HashSet internal use map to save data, will be hashset data as the key value of the map to save, which is the reason why elements in hashset cannot be duplicated. and the map to save the key value, will be to determine whether the current map contains the key object, the interior is first through the key hashcode, determined to have the same hashcode, and then by the Equals method to determine whether the same.

Java Basics Tutorial: Comparison of Hashtable and HashMap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.