Java Hashtable Class

Java 数据结构Java Data Structures


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

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

Like HashMap, Hashtable stores key/value pairs in a hash table. When using a hash table, you specify the object to be used as a key, and the value to be linked to that key.

Then, the key is hashed, and the resulting hash code is used as the index of the value stored in the table.

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

Hashtable()

The second constructor creates a hash table with the specified size:

Hashtable(int size)

The third constructor creates a hash table with the specified size and specifies the fill ratio via fillRatio.

The fill ratio must be between 0.0 and 1.0, and it determines how full the hash table can become before it is resized:

Hashtable(int size,float fillRatio)

The fourth constructor creates a hash table initialized with the elements in M.

The capacity of the hash table is set to twice the size of M.

Hashtable(Map m)

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

No. Method Description
1 void clear( )
Clears this hash table so that it contains no keys.
2 Object clone( )
Creates a shallow copy of this hash table.
3 boolean contains(Object value)
Tests whether a key is associated with the specified value in this hashtable.
4 boolean containsKey(Object key)
Tests whether the specified object is a key in this hash table.
5 boolean containsValue(Object value)
Returns true if this Hashtable maps one or more keys to this value.
6 Enumeration elements( )
Returns an enumeration of the values in this hash table.
7 Object get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. More precisely, if this map contains a mapping from key k to value v that satisfies (key.equals(k)), then this method returns v; otherwise, it returns null.
8 boolean isEmpty( )
Tests whether this hash table has no keys mapped to values.
9 Enumeration keys( )
Returns an enumeration of the keys in this hash table.
10 Object put(Object key, Object value)
Maps the specified key to the specified value in this hash table.
11 void rehash( )
Increases the capacity of this hash table and reorganizes it internally so that its elements can be accommodated and accessed more efficiently.
12 Object remove(Object key)
Removes 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 this Hashtable object, which is a set of entries enclosed in braces and separated by the ASCII characters ', ' (comma and space).

Example

The following program demonstrates several methods supported by this data structure:

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 result of compiling and running the above example is as follows:

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

Zara's new balance: 4434.34

Java 数据结构Java Data Structures

Other Extensions