The implementation of a hash table is often called hashing. Hashing is a technique used to perform insertions, deletions, and lookups of constant average time. However, tree operations that require ordering of element information are not supported. So for example, findmax,findmin and the sort traversal of these operations are not supported by hashing.
If an element is hashed with an inserted element (such as an array ordinal of a hash list, and many elements are inserted into the same sequence number), then a conflict is created, which needs to be eliminated. There are two ways to resolve conflicts:
1 Separate Link method
2 Open addressing methods (including linear probing, square probing, double hashing)
Both may need to be hashed again.
The following shows the separation link method, the code:
Package Com.itany.separatechainning;import java.util.linkedlist;import java.util.list;/* * Split link method when resolving conflict first method It does this by hashing to the same worth of all elements in a single table. * To perform a lookup we use the hash function to determine exactly which list to traverse, and then we will perform a lookup in the linked list once. * Hash table stores a list of array */public class separatechaininghashtable<t>{private static final int default_table_size=101; We define the fill factor for the hash table: the number of elements in the hash table and the hash table size is 1, i.e. the average length of the set list is 1 private int currentsize; Private list<t>[] thelists; Public separatechaininghashtable () {this (default_table_size); } public separatechaininghashtable (int size) {////list in array is initialized thelists=new linkedlist[nextprime (size )]; for (int i=0;i<thelists.length;i++) {thelists[i]=new linkedlist<t> (); }} public void Makeempty () {for (list<t> list:thelists) {list.clear (); } currentsize=0; } public void Insert (T t) {list<t> Whichlist=thelists[myhash (t)]; if (!contains (t)) {Whichlist.add (t); currentsize++; if (currentsize>thelists.length) ReHash (); }} public Boolean contains (T-t) {//Myhash find out which collection is list<t> Whichlist=thelists[myhash (t)]; return Whichlist.contains (t); } public void remove (T t) {list<t> Whichlist=thelists[myhash (t)]; if (contains (t)) {Whichlist.remove (t); currentsize--; }} private int Myhash (T t) {int hash=t.hashcode (); For each t get the ordinal size of the array from 0-thelists.length-1 to allocate hash%=thelists.length; Prevent hash value as negative if (hash<0) hash+=thelists.length; return hash; }//There is a case where the currentsize>thelists.length needs to expand the array and then hash//Because the list is too full then the operation time will become longer, and the insert operation may fail the method at this time to create another twice times the size of the table//and Use a new related hash function (because tablesize changes when calculating), scan the entire original hash list, calculate new hash values for each element and insert them into the new table private void ReHash () {list<t>[] O ldlists=thelists;//Copy Awill have to use thelists in re-thelists=new Linkedlist[nextprime (2*thelists.length)]; for (int i=0;i<thelists.length;i++) {thelists[i]=new linkedlist<t> (); }//Copy the original element into the new array note that the elements in the collection are copied in for (int i=0;i<oldlists.length;i++) {for (T T:OLDL Ists[i]) {insert (T); }}}//The size of the table is a prime number this can guarantee a good distribution//Whether it is a prime number private static Boolean isprime (int num) {int I=1; while ((num% (i+1))!=0) {i++; } if (I==num-1) {return true; } else {return false; }} private static int nextprime (int num) {while (!isprime (num)) {num++; } return num; } }
Package com.itany.separatechainning;//example of an employee class that can be placed in a hash table */* * To put a class in a hash table must provide two methods, one is the Equals method Because equals is used to compare when looking for the contains method in the list's collection * There is also a hashcode method because it is necessary to find out which collection the employee object should be placed in (find the corresponding ordinal of the array) */public Class employee{ private String name; Public Employee (String name) { this.name=name; } @Override public boolean equals (Object obj) { return obj instanceof Employee && name.equals (( Employee) (obj). name); } public int hashcode () { //string class is having its own Hashcode method return Name.hashcode (); } /* * String Class own Hashcode method * public int hashcode () { int h = hash; if (h = = 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = + * H + val[i]; } hash = h; } return h; } */}
Package Com.itany.separatechainning;public class test{public static void Main (string[] args) { Employee E1=new Employee ("Zhangsan"); Employee E2=new employee ("Lisi"); Employee E3=new Employee ("Wangwu"); Employee E4=new Employee ("Zhaoliu"); Separatechaininghashtable<employee> sc=new separatechaininghashtable<employee> (); System.out.println (Sc.contains (E1)); Sc.insert (E1); System.out.println (Sc.contains (E1)); Sc.remove (E1); System.out.println (Sc.contains (E1)); } }
Results:
False
True
False
Data structure--resolving hash conflicts, separating link method