Algorithm 6-2: Independent linked list for solving ha conflicts

Source: Internet
Author: User

An independent linked list is a way to solve hash conflicts. The basic idea is to put several objects whose hash values conflict with each other in a linked list.


Code

Public class HashST
 
  
{Private static class Node {Object key; // because you cannot create a Generic Array, you can only set the Object to the Object class Object value; Node next; public Node (Object key, Object value, node next) {this. key = key; this. value = value; this. next = next ;}} private Node [] map; private static final int M = 97; public HashST () {map = new Node [M];} public Value get (Key key) {int hash = hash (key); Node node = map [hash]; while (node! = Null) {if (key. equals (node. key) {return (Value) node. value;} node = node. next;} // return null not found;} public void put (Key key, Value value) {int hash = hash (key); Node node = map [hash]; while (node! = Null) {if (key. equals (node. key) {node. value = value; return ;}} map [hash] = new Node (key, value, node);} private int hash (Key key) {return (key. hashCode () & 0x7fffffff) % M ;}}
 


Performance

Performance is related to M. M is the number of linked lists. If M is too large, there will be many empty linked lists in the memory. If M is too small, each linked list will be very long, resulting in poor performance. Therefore, M is generally N/5, and N is the number of keywords.


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.