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.