HashMap、HashTable,ConcurrentHashMap

Wherein the data structure is composed of an array of added chain, key array, value of the list

In Java, save the data, there are two relatively simple data structures: arrays and linked lists. Features of the array is: Addressing easy, difficult to insert and delete; and the list of features are: addressing difficult, insertions and deletions easy. We mentioned above, the conflict commonly used hash function solution there is a method called a chain address law, in fact, the arrays and linked lists together, play to the advantage of both, we can be understood as an array of linked lists .

                                

We can see from the chart, it is clear that the left is an array, each member of the array is a linked list. All elements of the data structure contains a pointer contained, for links between the elements. According to our own characteristic elements of the element assigned to a different list to go, in turn, is what we find the right list these features, then find the correct element from the list. Wherein, the subject method is calculating hash algorithm, i.e., the protagonist of this hash () function of the array elements (of course, further comprising the indexOf () function) according to element characteristics.

 

Computing element array subscript characterized by: hash ()

  HashMap is the length of the array, Java in a predetermined multiple of this length is only 2, initial value 16.

  Seeking hash simple approach is taken to find the key hashcode,, then the value of int hashcode obtained modulo the length of the array by calling the hashCode object (). For performance reasons, Java uses the total realized operation bitwise modulo operation.

 

ConcurrentHashMap is inherited from HashMap, commonly used in a thread-safe, strong performance in HashMap

to sum up

  • HashMap default initialization size is 16, after each expanded to 2 times the original.

  • HashTable default initial size is 11, expanded to the original after each 2n + 1.

  • When the size of the hash table is a prime number, a simple modulo hash result will be more uniform, so that from this point alone, the HashTable hash table size selection, out of these seems to be higher. Because the hash result of the more dispersed the better.

  • When modulo calculation, if the modulus is a power of 2, then we can use bit operations directly to obtain result, the efficiency is much higher than do the divisions. So from the hash calculation efficiency is even better HashMap.

  • However, in order to improve efficiency in the use HashMap bit operations instead of hash, which in turn introduces a problem of uneven distribution of hash, so HashMap to solve this problem, but also on the hash algorithm has made some improvements, were disturbed calculated (and the high ground low combine to ensure that every value in the operation are involved, different high, then low bit different operations appear hash collision phenomenon does not appear).

  • Hashmap and allow key value is a null value, it is determined by whether the method comprising containsKey containsValue and corresponds to the key-value; the HashTable key-value pairs can be empty or null pointer exception packets.
  • HashMap class inherits from AbstractMap. But we have achieved Map interface.
    Dictionary class inherited from Hashtable, Dictionary class is a class that has been discarded (Note reflected in the source code). Parent classes are abandoned, naturally no one used it a subclass of Hashtable.
  • Thread-safe Hashtable, HashMap thread-safe
  • Conflict resolution in jdk1.8 not the same
    •   

      1. If the number of conflicts is less than 8, the list is based on conflict resolution.
      Entry 2. When a conflict is equal to greater than 8, it will be stored as the converted conflict ** red-black tree. **
      3. When the number but less than 6, and stored into the list.

    • In the HashTable, they are stored in a linked list.

Guess you like

Origin www.cnblogs.com/nyhhd/p/12643148.html