Java Study Notes (20) -- Java hash _ algorithm content

Source: Internet
Author: User

[Previous] On weekends, I was planning to find someone to play with, but I couldn't find it, so I had to learn. Why do I need to use HashMap to learn the hash list? During project creation, DTO needs to be transmitted on the server and client, and the transmitted attributes are dynamically added. Therefore, HashMap needs to dynamically add some attributes to the DTO class, so let's take a look. [Definition] Hash table: a data structure directly accessed Based on keywords. That is to say, the Hash table establishes a direct ing relationship between keywords and storage addresses. Hash function: a function that maps the keyword in the search table to the address corresponding to the keyword, and records it as Hash (key) = Addr. Conflict: The hash function may Map two or more different keywords to the same address, which is called "conflict ". Synonyms: these different keywords are used as synonyms. [Basic knowledge] 1. common hash functions: direct addressing -- H (key) = a * key + B Division remainder -- H (key) = key % p digital analysis method square de-France 2. methods to deal with conflicts (1) zipper refers to storing all synonyms in a linear linked list, which is uniquely identified by the hash address. Keyword code: {,}, hash function is H (key) = key % 13. The following table is created using the zipper method: (2) The open address method the open address method refers to the idle address that can store new table items and is developed for its synonym table items, it is also developed for its non-synonym table items. The mathematical recursive formula is (Hi indicates the hash address of the I-th probe after the conflict occurs): Hi = (H (key) + di) % m where, I =, ·, k (k <= m-1); m indicates the table length of the hash table; di indicates the incremental sequence. After an incremental sequence is determined, the corresponding processing method is determined. Usually there are: 1. Linear probing di =, M-1 a conflict occurs, view the next unit in sequence in the table until an empty unit is found. 2. square test di = 12,-12, 22,-22 ·, k2,-k2. K <= m/2 3. then hash [question] with a set of keywords {9, 1,}, using the hash function H (key) = key mod 7, the table length is 10, resolve the conflict by using the method Hi = (H (key) + di) % 10 (di = 12, 22, 32. Requirement: Construct a hash list for the sequence of keywords and calculate the average length of the queried results. Answer: {9, 1,} mod 7 = {, 6,} keyword Calculation Comparison times 9 H (9) = 9% 7 = 2 (no conflict) 1 1 H (1) = 1% 7 = 1 (no conflict) 1 23 H (23) = 23% 7 = 2 (conflict ), h1 = (2 + 1) % 10 = 3 (no conflict) 2 14 H (14) = 14% 7 = 0 (no conflict) 1 55 H (55) = 55% 7 = 6 (no conflict) 1 20 H (20) = 20% 7 = 6 (conflict), H1 = (6 + 1) % 10 = 7 (no conflict) 2 84 H (84) = 84% 7 = 0 (conflict), H1 = (0 + 1) % 10 = 1 (conflict), H2 = (0 + 22) % 10 = 4 (no conflict) 3 27 H (27) = 27% 7 = 6 (conflict), H1 = (6 + 1) % 10 = 7 (conflict ), h2 = (6 + 22) % 10 = 0 (conflict), H3 = (6 + 32) % 10 = 5 (no conflict) 4 hash address 0 1 2 3 4 5 6 7 8 9 keyword 14 1 9 23 84 27 55 20 Probe length 1 1 1 2 3 5 1 2 average search length = (1 + 1 + 2 + 1 + 1 + 2 + 3 + 4) /8 = 15/8

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.