The difference between HashMap and Hashtable in Java collection

1.1 Similarities between HashMap and Hashtable (important)

  • Both HashMap and Hashtable are hash tables that store “key-value pairs” , both of which are implemented using the zipper method

    The idea of ​​storage is: through table array storage, each element of the array is an Entry ; and an Entry is a one-way linked list , each node in the Entry linked list stores key-value key-value pair data

  • Steps to add key-value key-value pairs (important)

    First, calculate the hash value according to the key value, and then calculate the array index (that is, the index of the key-value in the table) . Then, find the Entry (that is, a one-way linked list) according to the array index, and then traverse the one-way linked list , and compare the key with the key of each node in the linked list. If the key already exists in the Entry list, replace the old value with the value; if the key does not exist in the Entry list, create a new key-value node and insert the node into the header position of the Entry list

  • Steps to delete key-value key-value pairs

    Deleting key-value pairs is much simpler than adding key-value pairs. First, calculate the hash value according to the key, and then calculate the array index (that is, the index of the key-value in the table) . Then, find the Entry (ie, one-way linked list) according to the index . If the node key-value exists in the linked list Entry, delete the node in the linked list

1.2 Similarities and differences between HashMap and Hashtable

1.2.1 Different inheritance and implementation

  HashMap inherits from AbstractMap and implements Map, Cloneable, java.io.Serializable interfaces

public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable { ... }

  Hashtable inherits from Dictionary and implements Map, Cloneable, java.io.Serializable interfaces

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable { ... }

1.2.2 Different thread safety

  • Almost all functions of Hashtable are synchronized, that is, it is thread-safe and supports multi-threading
  • HashMap function is asynchronous, it is not thread-safe . To use HashMap in multiple threads, we need to perform additional synchronization. For synchronization of HashMap, you can use the synchronizedMap static method provided by the Collections class, or directly use the ConcurrentHashMap class in the java.util.concurrent package provided after JDK 5.0

1.2.3 Different treatment of null values

  • HashMap key and value can be null

    When the key of HashMap is null, HashMap will insert it into the fixed position of table [0] (that is, the first position of HashMap hash table); and table [0] will only contain a value whose key is null. When multiple values ​​with a key of null are inserted, table [0] will retain the last inserted value

  • Hashtable's key and value cannot be null

    Otherwise, an exception NullPointerException will be thrown

1.2.4 Different traversal methods supported

  • HashMap only supports Iterator (iterator) traversal
  • Hashtable supports Iterator (Iterator) and Enumeration (enumerator) traversal in two ways

1.2.5 When traversing through Iterator, the traversal order is different

  • HashMap is a "front to back" traversal array; then for a linked list corresponding to a specific item of the array, traverse from the table head
  • Hashtabl is a "from back to front " traversal array; then for a linked list corresponding to a specific item in the array, traverse from the table head

1.2.6 Different hash value algorithm when adding key-value

  • HashMap uses a custom hash algorithm when adding elements
  • Hashtable does not have a custom hash algorithm, but directly uses the hashCode () of the key

Guess you like

Origin www.cnblogs.com/flyingrun/p/12746232.html