The implementation of a hash table is often called a hash hashing. Hashing is a technique used to perform insertions, deletions, and lookups with constant average time. However, tree operations that require any sort of information between elements will not be supported effectively. The ideal hash table data structure is simply a fixed-size array with some items. Usually a lookup is done on a part of the item (the data field), which is called the keyword. For example, an item can consist of a string (as a keyword) and some other data fields. We take the size of the table as a tablesize and interpret it as part of the hash data structure, not just a variable that floats on the global scale. The usual habit is to make the table change from 0 to TableSize-1.
Each keyword is mapped to a number in the range from 0 to TableSize-1, and is placed in the appropriate cell. This mapping is called a hash function, ideally it should be simple to calculate, and should ensure that any two different keywords are mapped to different units. However, this is not possible, because the number of units is limited, and the keywords are actually used. Therefore, we look for a hash function that distributes the keywords evenly between the cells.
This is the basic idea of hashing, and the rest of the problem is to choose a function that determines what should be done when two keywords are hashed to the same worth of time (that is, conflicts) and how to determine the size of the hash table.
If the input keyword is an integer, it is generally reasonable to return the key mod tablesize directly, unless the key happens to have some non-conforming nature. In this case, the selection of the hash function needs to be carefully considered. For example, if the size of a table is 10 and the keyword is 0 bits, then the above standard hash function is not good. To avoid the above situation, a good method is usually to ensure that the size of the table is a prime number. When the input keyword is a random integer, the hash function is simple to calculate and the keyword is distributed evenly. Typically, the keyword is a string, in which case the hash function needs to be carefully selected. The rest of the main programming details are to resolve the conflict elimination problem. When an element is inserted and hashed to the same value as an already inserted element, a conflict is created, and the conflict needs to be eliminated. There are several ways to solve this kind of conflict, here we mainly introduce the method of separating link and open addressing.
The detach chaining method is to persist all elements that are hashed to the same value into a table. We can use the standard library table implementation method. If the space is tight, it is preferable to avoid using them (because the tables are two-way linked and waste space). To perform a lookup, we use a hash function to determine exactly which linked list to traverse, and then we see in the corresponding linked list whether the element is in the appropriate position (if you allow the insertion of duplicate elements, it is usually necessary to set aside an additional domain, which increases by 1 when a matching event occurs). If this element is a new element, it will be inserted into the front of the list, not only because it is convenient, but also because it often happens that the newly inserted element is most likely to be accessed soon. The following is a simple implementation of a hash table using the Detach link method:
Import Java.util.iterator;import java.util.linkedlist;import Java.util.list;import Java.util.random;public class separatechaininghashtable<anytype> {private static final int default_table_size = 10;//default Capacity private list< Anytype>[] thelists;//array private int currentsize;//number of current data public separatechaininghashtable () {This (default_ table_size);} public separatechaininghashtable (int size) {thelists = new Linkedlist[nextprime (size)];for (int i = 0; i < Thelists.len Gth i++) {Thelists[i] = new linkedlist<anytype> ();}} /** * make hash table empty */public void Makeempty () {for (list<anytype> list:thelists) {list.clear ();} currentsize = 0;} /** * Hash table contains an element * @param x Query element * @return Query result */public Boolean contains (AnyType x) {list<anytype> whichlist = Thel Ists[myhash (x)];return whichlist.contains (x);} /** * Inserts an element into the hash table, if present does not operate * @param x Insert element */public void Insert (AnyType x) {list<anytype> whichlist = Thelists[myhash ( x)];if (!whichlist.contains (x)) {whichlist.add (x); if (++currentsize > Thelists.length) {rehash ();}} else {}}/** * Removes an element from the hash table and does not operate if it does not exist * @param x Delete element */public void Remove (AnyType x) {list<anytype> whichlist = thelist S[myhash (x)];if (Whichlist.contains (x)) {whichlist.remove (x); currentsize--;} else {}}/** * hash algorithm, there are several implementations * @param x Elements * @r Eturn hash value */private int myhash (AnyType x) {int hashval = X.hashcode (); Hashval%= thelists.length;if (Hashval < 0) {hash Val + = Thelists.length;} return hashval;} /** * Re-hash function, when inserting space is insufficient, execute */private void rehash () {list<anytype>[] oldlists = thelists;//Allocate an empty table of twice times the size of thelists = new List [NextPrime (2 * thelists.length)];for (int j=0;j<thelists.length;j++) {thelists[j]=new linkedlist<anytype> () ;} CurrentSize = 0;for (int i = 0; i < oldlists.length; i++) {for (AnyType item:oldlists[i]) {Insert (item);}}} /** * Check if an integer is a prime number * @param num Check integer * @return Check results */private static boolean isprime (int num) {if (num = = 2 | | num = 3) {RE Turn true;} if (num = = 1 | | num% 2 = = 0) {return false;} for (int i = 3; I * I <= Num i + = 2) {if (num% i = = 0) {return false;}} return true;} /** * Returns a prime number that is not less than an integer * @param num integer * @return the next prime number (can be equal) */private static int nextprime (int num) {if (num = = 0 | | num = = 1 || num = = 2) {return 2;} if (num% 2 = = 0) {num++;} while (!isprime (num)) {num + = 2;} return num;} /** * Output Hash list */public void printtable () {for (int i=0;i<thelists.length;i++) {System.out.println ("-----"); Iterator Iterator=thelists[i].iterator (); while (Iterator.hasnext ()) {System.out.print (Iterator.next () + "");} System.out.println ();}} public static void Main (string[] args) {Random random = new random (); separatechaininghashtable<integer> hashTable = new separatechaininghashtable<integer> (); for (int i = 0; I & Lt 30; i++) {Hashtable.insert (Random.nextint (30));} Hashtable.printtable ();}}
Execution Result:-----
0
-----
1 24
-----
25 2
-----
3
-----
4 27
-----
5
-----
6 29
-----
7
-----
8
-----
-----
10
-----
-----
-----
13
-----
-----
15
-----
-----
17
-----
18
-----
19
-----
20
-----
21st
-----
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure (Java language)--hashtable simple implementation