Hash tables and their applications in Java

  • 2020-04-01 03:54:35
  • OfStack

A hash table, also known as a hash table, is a collection class structure used to store group objects.

What is a hash table

Arrays and vectors can store objects, but the object's storage location is random, meaning that there is no necessary connection between the object itself and its storage location. When an object is to be found, it can only be compared with the elements in a certain order (such as sequential search or binary search). When there are many elements in an array or vector, the efficiency of the search will be significantly reduced.

An efficient way to store records is to access them without comparing them with other elements. This requires a specific correspondence (set as f) between the storage location of the object and the key properties of the object (set as k), so that each object corresponds to a unique storage location. When searching, you simply calculate the value of f(k) based on the key attribute k of the object in question. If the object is in the collection, it must be at the storage location f(k), so there is no need to compare it with other elements in the collection. This correspondence f is called a hash method, and the table constructed according to this idea is called a hash table.

Java USES a Hashtable class to implement a Hashtable. Here are some concepts related to hashtables:

The & # 8226; Capacity: the Capacity of a Hashtable is not fixed and can grow automatically as objects are added.
The & # 8226; Key: each stored object needs to have a Key, which can be either the object itself or a part of the object (such as an attribute). All keywords in a Hashtable are required to be unique.
The & # 8226; Hash Code: to store an object on a Hashtable, you need to map its key key to an integer, which becomes the Hash Code for the key.
The & # 8226; Item: each Item in the Hashtable has two fields, the keyword field key and the value field (the stored object). Both Key and value can be of any Object type, but cannot be null.
The & # 8226; Load Factor: the Load Factor is expressed as the filling degree of the hash table, and its value is equal to the number of elements over the length of the hash table.

Use of hash tables

There are three main forms of hash table class constructors:

      Hashtable (); // default constructor with an initial capacity of 101 and a maximum filling factor of 0.75
      Hashtable (int capacity);
      Hashtable (int capacity, float loadFactor)
The main methods of the hash table class are shown in tables 8-6.

Table 8-6 common methods for hash table definitions

methods function
void clear() Reset and empty the hash table
boolean contains(Object value) Determines whether the hash table contains the given object, if any true Otherwise return false
boolean containsKey(Object key) Determines whether the hash table contains the given keyword, and if so returns true Otherwise return false
boolean isEmpty() Verify that the hash table is empty and if so returns true Otherwise return false
Object get(Object key) Gets the object corresponding to the keyword and returns if it does not exist null
void rehash() Rehash, expand the hash table to hold more elements, and when the hash expression reaches saturation, the system automatically calls this method
Object put(Object key,Object value) Saves the object to a hash table with the given keyword, where neither the keyword nor the element is null
Object remove(Object key) Removes an object corresponding to a given keyword from the hash table and returns it if it does not exist null
int size() Returns the size of the hash table
String toString() Converts the contents of the hash table to a string

The creation of a hash table can also be done through the new operator. Its statement is:

      HashTable has = new HashTable ();

Example:

Traversal of a hash table.


//********** 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 results:


2
1
3
12.3


Related articles: