A hash table, also known as a hash list, is a collection class structure used to store group objects.
What is a hash table
Both arrays and vectors can store objects, but the objects are stored in random locations, meaning there is no necessary connection between the object itself and its storage location. When you are looking for an object, you can only compare it in some order, such as a sequential lookup or a binary lookup, and the efficiency of the lookup is significantly reduced when the number of elements in the array or vector is large.
An effective way of storing is not compared with other elements, one access can get the required records. This requires establishing a specific correspondence between the object's storage location and the object's key properties (set to K) so that each object corresponds to a unique storage location. When looking, simply calculate the value of f (k) based on the key attribute K of the unknown Origin object. If this object is in the collection, it must be on the storage location F (k), so it does not need to be compared with other elements in the collection. This correspondence is called the hash (hash) method, and the table established by this thought is a hash.
Java uses a hash table class (Hashtable) to implement a hash table, and here are some concepts related to the hash table:
• Capacity (capacity): The capacity of the Hashtable is not fixed, and its capacity can grow automatically with the addition of the object.
• Keywords: Each stored object needs to have a keyword, either the object itself or a part of the object (such as a property). All the keywords in a Hashtable are required to be unique.
• Hash code: To store an object on a Hashtable, you need to map its keyword key to an integer data that becomes the hash code of the key.
• Item: Each item in the Hashtable has two fields, namely the key domain key and the range value (the stored object). Both the Key and value can be arbitrary objects of type object, but cannot be empty.
• Reload factor (Load Factor): The fill factor is expressed as the full extent of the hash table, whose value equals the length of the hash table on the number of elements.
Use of hash tables
There are three ways to construct a hash table class:
Hashtable (); Default constructor, initial capacity is 101, Max fill factor 0.75
Hashtable (int capacity);
Hashtable (int capacity,float loadfactor)
The main methods of the Hashtable class are shown in table 8-6.
Table 8-6 Common ways to define hash tables
Method |
function |
void Clear () |
Reset and empty the hash table |
Boolean contains (Object value) |
Determines whether the given object is contained within the hash table and returns False if it returns true |
Boolean ContainsKey (Object key) |
Determines whether the given keyword is contained within the hash table and returns False if it returns true |
Boolean IsEmpty () |
Verify that the hash table is empty and return true if it returns false |
Object get (Object key) |
Gets the object of the corresponding keyword, if no return null exists |
void Rehash () |
Hash, expand the hash table so that it can save more elements, when the hash table reaches saturation, the system automatically call this method |
Object put (object Key,object value) |
Saves the object to a hash table with the given keyword, and the keywords and elements here are not nullable |
Object remove (Object key) |
Removes the object corresponding to the given keyword from the hash table if the object does not exist and returns null |
int size () |
Returns the size of the hash table |
String toString () |
Converting the contents of a Hashtable to a string |
The creation of a hash table can also be implemented with the new operator. Its statement is:
HashTable has=new HashTable ();
Example:
The traversal of the "example 8-12" hash table.
?
123456789101112131415 |
//********** 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
));
has.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()));
}
}
}
|
Operation Result:
?
Hash table in Java and its application in detail