Latest web development tutorials

Java Hashtable Interface

Hashtable is part of the original java.util is a Dictionary concrete implementation.

However, Java 2 reconstruction Hashtable implements the Map interface, so, Hashtable is now integrated into the collections framework. It HashMap class is very similar, but it supports synchronization.

Like Like HashMap, Hashtable in a hash table to store key / value pairs. When using a hash table, to specify the object as a key, and you want to link to the value of the bond.

Then, the key is hashed and the resulting hash code is used as an index to store the values ​​in the table.

Hashtable defines four constructor. The first is the default constructor:

Hashtable()

The second constructor creates a hash table specified size:

Hashtable(int size)

The third constructor creates a specified size of the hash table, and specify the fill proportion by fillRatio.

Filling ratio must be between 0.0 and 1.0, which determines the full extent of the hash table before re-sizing:

Hashtable(int size,float fillRatio)

The fourth constructor creates an element to the M element in the hash table is initialized.

The capacity of the hash table is set to double M's.

Hashtable(Map m)

Hashtable in addition to the methods defined in the Map interface, but also defines the following methods:

No. Method Description
1 void clear ()
Clears this hashtable so that it contains no keys.
2 Object clone ()
Creates a shallow copy of this hash table.
3 boolean contains (Object value)
Whether there is a value associated with the specified key test this mapping table.
4 boolean containsKey (Object key)
Tests if the specified object is a key in this hashtable.
5 boolean containsValue (Object value)
If this Hashtable maps one or more keys to this value, it returns true.
6 Enumeration elements ()
Returns a hash table enumeration values.
7 Object get (Object key)
Returns the specified key is mapped to the value, if this map contains no mapping for this key, it returns null. More formally, if this map contains (key.equals (k)) from the key k to a value v Mapping , then this method returns v; otherwise it returns null.
8 boolean isEmpty ()
Tests if this hashtable maps no keys to values.
9 Enumeration keys ()
Returns an enumeration of the hash table keys.
10 Object put (Object key, Object value )
The specified key is mapped to the hash table specified value.
11 void rehash ()
Increase the capacity of the hash table and its internal reorganization to better accommodate and access its elements.
12 Object remove (Object key)
Remove the key and its corresponding value from the hash table.
13 int size ()
Returns the number of keys in this hash table.
14 String toString ()
Returns a string representation of the Hashtable object in the form of ASCII characters "," (comma and space) separated, enclosed in braces and a set of entries.

Examples

The following program illustrates this data structure supports several methods:

import java.util.*;

public class HashTableDemo {

   public static void main(String args[]) {
      // Create a hash map
      Hashtable balance = new Hashtable();
      Enumeration names;
      String str;
      double bal;

      balance.put("Zara", new Double(3434.34));
      balance.put("Mahnaz", new Double(123.22));
      balance.put("Ayan", new Double(1378.00));
      balance.put("Daisy", new Double(99.22));
      balance.put("Qadir", new Double(-19.08));

      // Show all balances in hash table.
      names = balance.keys();
      while(names.hasMoreElements()) {
         str = (String) names.nextElement();
         System.out.println(str + ": " +
         balance.get(str));
      }
      System.out.println();
      // Deposit 1,000 into Zara's account
      bal = ((Double)balance.get("Zara")).doubleValue();
      balance.put("Zara", new Double(bal+1000));
      System.out.println("Zara's new balance: " +
      balance.get("Zara"));
   }
}

The above examples compiled results are as follows:

Qadir: -19.08
Zara: 3434.34
Mahnaz: 123.22
Daisy: 99.22
Ayan: 1378.0

Zara's new balance: 4434.34