Features and principles of the hash table

Also known as a hash table hash table, it is an amazing structure, the biggest feature is fast. There are many of its structure, the most popular and easiest to understand is: the structure of the order table + list. Is the length of the main structure may be sequentially dynamic table, each node in the sequence table can be drawn with a single linked list. Principle hash table can be explained from the following three points.

Add Data Principle:

1), calculates the hash code, calling hashCode () method, the result is an int integer hash code itself can be taken

2) The calculation of the hash code storage location (index of the array) [y = k (x) (in addition to specimens from Method I) stored hash table]

3), the data stored in the specified location, if there is already an element exists, that there is a conflict, we need to compare the list along with repeated elements, not stored. No, it is stored.

Conclusion: Adding fast. The time complexity of O (1); disorder.

Query principle of data:

And add the same process, or three steps to get. Conclusion: Query fast.

Summary: The magic lies in accordance with the contents of the hash table queries, ideally up to the time complexity of the query array index of O (1). The core lies in its inquiry is not based on the comparison, but based computing. When there is a conflict, it will reduce efficiency.

How to reduce conflict:

1) loading factors: length ratio of the number of records and tables in the hash table. Will exceed the loading factor expansion. Otherwise, the probability of conflict will be greatly improved, which could affect performance.

2) Select the hash function

Direct addressing method takes the square folding process other specimens hair method I

3) methods of dealing with conflict

Chain address method and then open the address hashing method to establish a common overflow area

hashCode and equals () to add the query role in the hash table:

1) hashCode (): calculated hash code is an integer, can be calculated position data stored in a hash table based on the hash code.

2) equals (): when you add a conflict, need to be compared by equals, to determine whether the same is also required when compared using equals inquiry to determine whether the same.

Guess you like

Origin www.cnblogs.com/sinoaccer/p/12099730.html