Magical hash table

Hash table is also called a hash table, a data structure that provides the key (Key) and value (value) mappings. As long as a given Key, it is possible to efficiently find it matches the value of Value, close time complexity O (1). Before the dissolution of the principles we look at the list of the hash function. Hash table is an array in nature. But only in accordance with the array subscripts, like a [0], a [1], a [3] in such a way to access, and the hash key is a string type is based. For example, students learn number as key, enter 00213, the query to John Doe; or by key word, input by, inquiry-to-digital 520 ... so we need a transit point, and the key array index by some way conversion, this junction is called a hash function. In different languages, the implementation of the hash function is not the same. Here the usual collection of HashMap java example, take a look at to achieve the hash function in java. In the java language and object-oriented majority, each object has its own hashcode, the hashcode is important to distinguish between different objects of identification, no matter what type of object itself is that their hashcode is an integer variable. Since they are integer variables, you want to convert the array index is not hard to realize the, JDK in a way hash function using bit operations to optimize performance.
Hash list of read and write operations. Write operation is to insert a new key pair in the hash table (called the Entry in the JDK), a first step, the key through a hash function to convert the array subscript, the position of the second step if an array subscript no corresponding elements, put the Entry into the filling position corresponding array subscript, but since the length of the array is limited, when the insert Entry rises, obtained by the different key marked with the hash function may be duplicated, this situation called a hash collision. Hash collision is unavoidable, but there is a solution, one is open addressing method, one is the list method. OpenAddressed law principle is very simple, when a key is obtained by a hash function corresponding array index has been occupied, we can find another job, looking for the next neutral position. In java, ThreadLocal used is an open-addressable. Another solution is to hash collision list method, this method is used in a HashMap them. Each array element is not only a HashMap Entry object, or a list of the first node, each of the object Entry Entry points to the next node through a next pointer, when a new array is mapped to the Entry conflicting positions, only can be inserted into the corresponding linked list.
Since the hash table is an array-based implementation, the hash table also involved the issue of expansion, when after several elements are inserted, the hash table reaches a certain saturation, the probability of key mapping position of conflict will gradually increase, so that, the same number of elements in the array subscript crowded location, the formed long chain, and the subsequent insertion query performance has a great impact. Then the hash table need to expand its length, that is, for expansion. For the implementation class HashMap JDK in the hash table, its expansion of influence factors there are two:
Capacity. HashMap i.e. current length, loadFactor, i.e. HashMap load factor, the default value is 0.75f. Expansion of the operation of the hash table to go through two steps: 1. Create an empty Entry empty array, the length is twice the original. 2, re-Hash, traversing the original Entry array, all the re-Hash Entry to the new prime group, why should re-Hash of it? Because after the length of the expansion, Hash rules also changed. After expansion, the original hash table crowded again become sparse, the original Entry also been re-assigned as uniform as possible. These are the various principles of operation of the hash table, be noted that, with regard to HashMap, JDK8 the previous version and has a big difference, when multiple Entry is Hash to the same array index, in order to enhance the efficiency of insertion and lookup , HashMap will Entry into a linked list data structure such a red-black tree. More details later on to beat the red-black tree family supplement.

Published 13 original articles · won praise 10 · views 399

Guess you like

Origin blog.csdn.net/qq_41426449/article/details/104495051