The hash table is also known as the distribution list, which is a collection class structure used to store group objects.
What is a hash table
Both arrays and vectors can store objects, but the storage position of the object is random, that is, there is no necessary connection between the object itself and its storage position. When you want to find an object, you can only compare with each element in a certain order (such as sequential search or two -point search). When the number of elements in an array or vector is large, the efficiency of the search will be significantly reduced.
A effective storage method is that it does not compare with other elements, and the record that can be obtained at a time can be obtained. This requires establishing a specific corresponding relationship between the storage position of the object and the key attribute of the object (set to K) to correspond to each object corresponding to a unique storage position. When searching, just calculate the value of f (k) based on the key attributes of the object to be checked. If this object is in the collection, it must be on the storage position f (k), so it is not necessary to compare with other elements in the set. Called this corresponding relationship F as a hash method, and the table established in accordance with this idea is a hash table.
Java uses the hashtable category to achieve the hash table. The following are some concepts related to the hash table:
• Capacity: The capacity of Hashtable is not fixed, and the capacity of the object can also increase automatically.
• Key (key): Each storage object requires a keyword. Key can be an object itself or part of the object (such as a certain attribute). All keywords in a Hashtable are unique.
• Hash Code: If you want to store the object to Hashtable, you need to map the keyword key to a integer data to become the hash code of Key.
• Item: Each of Hashtable has two domains, which are keyword domain Key and value domain Value (storage object). Both Key and Value can be an Object type object, but it cannot be empty.
• Load Factor: The filling factor is represented by the fullness of the hash table, and its value is equal to the length of the number of elements than the above hash table.
Hash table use
There are three main forms of construction methods for hash tables:
Hashtable (); // The default constructor, the initial capacity is 101, the maximum filling factor 0.75
Hashtable (int capacity);
Hashtable (int capacity, float loadFactor)
The main methods of hash tables are shown in Table 8-6.
Table 8-6 Frequently methods defined by hash table definition
method | Function |
---|---|
void Clear () | Re -set and clear the hash table |
Boolean Contains (Object Value) | Determine whether the hash table contains a given object, if there is a return True, otherwise the False will be returned |
Boolean Containskey (Object Key) | Determine whether the hash table contains a given keyword, if there is a return to True, otherwise the False will be returned |
boolean isempty () | Confirm whether the hash table is empty, if you return to True, otherwise the False will be returned |
Object Get (Object Key) | Object to obtain the corresponding keywords, if there is no return NULL |
void rehash () | No matter howh, the expansion hash table can save more elements. When the hash table reaches saturated, the system automatically calls this method |
Object Put (Object Key, Object Value) | Save the object to the hash table with a given keyword. The keywords and elements here must not be empty |
Object Remove (Object Key) | Delete the object corresponding to the keyword phase from the hash table, if the object does not return to NULL |
int size () | Back to the size of the hash table |
String tostring () | Convert the content of the hash table to a string |
The creation of the hash table can also be implemented through the NEW operator. Its statement is:
Havetable has = new hashtable ();
example:
[Example 8-12] traversing of hash tables.
// ************ ep8_12.java ******************************************************************************************************** import java.util.*; Class EP8_12 {Public Static void Main (String ARGS []) {Hashtable Has = New Hashtable (); Has.put ("One", New Integer (1)); Has.put ("TWO", New Integer (2)); Has.put ("Three", New Integer (3)); .put ("four", new double (12.3)); set s = has.keyset (); for (Iterator <string> i = s.iterator (); i.hasnext ();) {System.out.println (Has.get (I.NExt ()));}}}
Run results:
21312.3