hashtable 模板类

sdds
Class HashTable<K,V>

java.lang.Object
  sdds.HashTable<K,V>

Type Parameters:
K - the type of object that will act as keys for the hash table
V - the type of object that is being stored in the hash table
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
DHashTable, SHashTable
public abstract class HashTable<K,V>
    
    
    
    
extends java.lang.Object
implements java.lang.Cloneable

A simple hash-table (the abstract superclass of DHashTable and SHashTable).

The performance of each operation varies for DHashTable and SHashTable, but in both cases searching for a value based on its key takes O(1) amortized time (assuming the hash codes of keys are uniformly distributed).

Note that key/value mappings can be added to the hash table using == or Object.equals(java.lang.Object). Using add(K, V) and addEquals(K, V) on the same table may put it in an inconsistent state. For example, say that K1 is mapped to V1 using add(K, V) and K2 is mapped to V2 using add(K, V). If K1.equals(K2), remapping K1 using addEquals(K, V) could overwrite either one of the above mappings.

Author:
Stephen G. Ware

Field Summary
protected static int DEFAULT_EXPANSION_FACTOR
          The default number of times larger that the hash table will get when it needs to expand
protected static int DEFAULT_INITIAL_SIZE
          The default number of mapping that can exist in a new hash table
protected static float DEFAULT_LOAD_FACTOR_THRESHOLD
          The load factor at or above which the hash table will expand
protected  int size
          The number of mapping in this hash table
protected  sdds.HashTable.Node<K,V>[] table
          The buckets
 
Constructor Summary
  HashTable()
          Construct an empty hash table.
protected HashTable(int size, sdds.HashTable.Node<K,V>[] table)
          Construct a hash table with a given number of mappings and given set of buckets.
  HashTable(K[] keys, V[] values)
          Construct a hash table which initially contains mappings for a given set of keys and values.
 
Method Summary
abstract  HashTable<K,V> add(K[] keys, V[] values)
          Add multiple key/value mappings to the hash table.
abstract  HashTable<K,V> add(K key, V value)
          Add a key/value mapping to the hash table.
abstract  HashTable<K,V> addEquals(K[] keys, V[] values)
          Add multiple key/value mappings to the hash table.
abstract  HashTable<K,V> addEquals(K key, V value)
          Add a key/value mapping to the hash table.
abstract  HashTable<K,V> clone()
          Return a new hash table which Object.equals(java.lang.Object) this hash table but is not == to this hash table.
protected  sdds.HashTable.Node<K,V>[] cloneTable()
          Clone the buckets, including every link in every chain.
 boolean equals(java.lang.Object other)
          Check if this hash table is the same as another.
 V get(K key)
          Return the value mapped to a given key.
protected static int getBucket(java.lang.Object obj, sdds.HashTable.Node<?,?>[] table)
          Given an object that needs to be placed in a bucket, this function return the number of the bucket in which to place the object.
 V getEquals(K key)
          Return the value mapped to a given key.
protected  float getLoadFactor()
          Return the load factor of the hash table.
 java.util.Iterator<K> keys()
          Return an Iterator that will yield all the keys in the hash table.
protected  sdds.HashTable.Node<K,V>[] rehash()
          Expand the buckets into the number of current buckets times DEFAULT_EXPANSION_FACTOR.
protected  sdds.HashTable.Node<K,V>[] rehash(int buckets)
          Expand the buckets into buckets new buckets.
abstract  HashTable<K,V> remove(K key)
          Remove a key/value mapping from the hash table.
abstract  HashTable<K,V> remove(K[] keys)
          Remove multiple key/value mappings from the hash table.
abstract  HashTable<K,V> removeEquals(K key)
          Remove a key/value mapping from the hash table.
abstract  HashTable<K,V> removeEquals(K[] keys)
          Remove multiple key/value mappings from the hash table.
 int size()
          Return the number of mappings in the hash table.
 java.lang.String toString()
          Return a string representation of this hash table and all its elements.
 java.util.Iterator<V> values()
          Return an Iterator that will yield all the values in the hash table.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_INITIAL_SIZE

protected static final int DEFAULT_INITIAL_SIZE
The default number of mapping that can exist in a new hash table

See Also:
Constant Field Values

DEFAULT_EXPANSION_FACTOR

protected static final int DEFAULT_EXPANSION_FACTOR
The default number of times larger that the hash table will get when it needs to expand

See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR_THRESHOLD

protected static final float DEFAULT_LOAD_FACTOR_THRESHOLD
The load factor at or above which the hash table will expand

See Also:
Constant Field Values

size

protected int size
The number of mapping in this hash table

table

protected sdds.HashTable.Node<K,V>[] table
The buckets

Constructor Detail

HashTable

protected HashTable(int size,
                    sdds.HashTable.Node<K,V>[] table)

Construct a hash table with a given number of mappings and given set of buckets.

Parameters:
size - the number of mappings in the hash table
table - the buckets

HashTable

public HashTable()

Construct an empty hash table.

HashTable

public HashTable(K[] keys,
                 V[] values)

Construct a hash table which initially contains mappings for a given set of keys and values.

Mappings are added as if using add(K, V).

Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values
Method Detail

getBucket

protected static final int getBucket(java.lang.Object obj,
                                     sdds.HashTable.Node<?,?>[] table)

Given an object that needs to be placed in a bucket, this function return the number of the bucket in which to place the object.

Parameters:
obj - the object to place
table - the buckets
Returns:
the index of the bucket to use

getLoadFactor

protected float getLoadFactor()

Return the load factor of the hash table.

The load factor is defined as the number of mapping divided by the number of buckets.

Returns:
the load factor

rehash

protected final sdds.HashTable.Node<K,V>[] rehash(int buckets)

Expand the buckets into buckets new buckets. Re-map all existing mappings to their new buckets.

Parameters:
buckets - the new number of buckets
Returns:
the new buckets

rehash

protected final sdds.HashTable.Node<K,V>[] rehash()

Expand the buckets into the number of current buckets times DEFAULT_EXPANSION_FACTOR. Re-map all existing mappings to their new buckets.

Returns:
the new buckets

size

public int size()

Return the number of mappings in the hash table.

Returns:
the number of mappings

add

public abstract HashTable<K,V> add(K key,
                                   V value)

Add a key/value mapping to the hash table. If a mapping exists whose key is == to this key, it is replaced.

Parameters:
key - the object which can be used to retrieve the value
value - the object to be retrieved using the key
Returns:
a hash table exactly like this one, but with the mapping added

add

public abstract HashTable<K,V> add(K[] keys,
                                   V[] values)

Add multiple key/value mappings to the hash table. If mappings exist whose keys are == to the given keys, they are replaced.

Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Returns:
a hash table exactly like this one, but with the mappings added
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values

addEquals

public abstract HashTable<K,V> addEquals(K key,
                                         V value)

Add a key/value mapping to the hash table. If a mapping exists whose key Object.equals(java.lang.Object) this key, it is replaced.

Parameters:
key - the object which can be used to retrieve the value
value - the object to be retrieved using the key
Returns:
a hash table exactly like this one, but with the mapping added

addEquals

public abstract HashTable<K,V> addEquals(K[] keys,
                                         V[] values)

Add multiple key/value mappings to the hash table. If mappings exist whose keys are Object.equals(java.lang.Object) to the given keys, they are replaced.

Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Returns:
a hash table exactly like this one, but with the mappings added
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values

get

public V get(K key)

Return the value mapped to a given key. Keys are searched using ==.

Parameters:
key - the object that was mapped to the desired value
Returns:
the value

getEquals

public V getEquals(K key)

Return the value mapped to a given key. Keys are searched using Object.equals(java.lang.Object).

Parameters:
key - the object that was mapped to the desired value
Returns:
the value

remove

public abstract HashTable<K,V> remove(K key)

Remove a key/value mapping from the hash table. Keys are searched using ==.

Parameters:
key - the key of the key/value mapping to be removed
Returns:
a hash table exactly like this one, but with the mapping removed

remove

public abstract HashTable<K,V> remove(K[] keys)

Remove multiple key/value mappings from the hash table. Keys are searched using ==.

Parameters:
keys - the keys of the key/value mappings to be removed
Returns:
a hash table exactly like this one, but with the mappings removed

removeEquals

public abstract HashTable<K,V> removeEquals(K key)

Remove a key/value mapping from the hash table. Keys are searched using Object.equals(java.lang.Object).

Parameters:
key - the key of the key/value mapping to be removed
Returns:
a hash table exactly like this one, but with the mapping removed

removeEquals

public abstract HashTable<K,V> removeEquals(K[] keys)

Remove multiple key/value mappings from the hash table. Keys are searched using Object.equals(java.lang.Object).

Parameters:
keys - the keys of the key/value mappings to be removed
Returns:
a hash table exactly like this one, but with the mappings removed

keys

public java.util.Iterator<K> keys()

Return an Iterator that will yield all the keys in the hash table.

Returns:
an iterator of keys

values

public java.util.Iterator<V> values()

Return an Iterator that will yield all the values in the hash table.

Returns:
an iterator of values

equals

public boolean equals(java.lang.Object other)

Check if this hash table is the same as another.

Two hash tables are considered the same if they contain the same key/value mappings. Keys and values are compared using Object.equals(java.lang.Object).

The class of the hash table is not taken into account. In other words, a DHashTable can be equal to an SHashTable if they contain the same key/value mappings.

Overrides:
equals in class java.lang.Object
Parameters:
other - another hash table
Returns:
true is the hash tables are the same, false if the hash tables are different or if other is not a hash table

clone

public abstract HashTable<K,V> clone()

Return a new hash table which Object.equals(java.lang.Object) this hash table but is not == to this hash table.

Overrides:
clone in class java.lang.Object

cloneTable

protected sdds.HashTable.Node<K,V>[] cloneTable()

Clone the buckets, including every link in every chain.

Returns:
the cloned buckets

toString

public java.lang.String toString()

Return a string representation of this hash table and all its elements.

Overrides:
toString in class java.lang.Object

你可能感兴趣的:(hashtable 模板类)