public abstract class

PreHashedMap

extends AbstractMap<K, V>
java.lang.Object
   ↳ java.util.AbstractMap<K, V>
     ↳ sun.util.PreHashedMap<V>

Class Overview

A precomputed hash map.

Subclasses of this class are of the following form:

 class FooMap
     extends sun.util.PreHashedMap<String>
 {

     private FooMap() {
         super(ROWS, SIZE, SHIFT, MASK);
     }

     protected void init(Object[] ht) {
         ht[0] = new Object[] { "key-1", value_1 };
         ht[1] = new Object[] { "key-2", value_2,
                      new Object { "key-3", value_3 } };
         ...
     }

 }

The init method is invoked by the PreHashedMap constructor with an object array long enough for the map's rows. The method must construct the hash chain for each row and store it in the appropriate element of the array.

Each entry in the map is represented by a unique hash-chain node. The final node of a hash chain is a two-element object array whose first element is the entry's key and whose second element is the entry's value. A non-final node of a hash chain is a three-element object array whose first two elements are the entry's key and value and whose third element is the next node in the chain.

Instances of this class are mutable and are not safe for concurrent access. They may be made immutable and thread-safe via the appropriate methods in the Collections utility class.

In the JDK build, subclasses of this class are typically created via the Hasher program in the make/tools/Hasher directory.

See Also

Summary

Protected Constructors
PreHashedMap(int rows, int size, int shift, int mask)
Creates a new map.
Public Methods
Set<Entry<String, V>> entrySet()
Returns a Set view of the mappings contained in this map.
V get(Object k)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

This implementation iterates over entrySet() searching for an entry with the specified key.

Set<String> keySet()
Returns a Set view of the keys contained in this map.

This implementation returns a set that subclasses AbstractSet.

V put(String k, V v)
Associates the specified value with the specified key in this map (optional operation).

This implementation always throws an UnsupportedOperationException.

Protected Methods
abstract void init(Object[] ht)
Initializes this map.
[Expand]
Inherited Methods
From class java.util.AbstractMap
From class java.lang.Object
From interface java.util.Map

Protected Constructors

protected PreHashedMap (int rows, int size, int shift, int mask)

Creates a new map.

This constructor invokes the init method, passing it a newly-constructed row array that is rows elements long.

Parameters
rows The number of rows in the map
size The number of entries in the map
shift The value by which hash codes are right-shifted
mask The value with which hash codes are masked after being shifted

Public Methods

public Set<Entry<String, V>> entrySet ()

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns
  • a set view of the mappings contained in this map

public V get (Object k)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

This implementation iterates over entrySet() searching for an entry with the specified key. If such an entry is found, the entry's value is returned. If the iteration terminates without finding such an entry, null is returned. Note that this implementation requires linear time in the size of the map; many implementations will override this method.

Parameters
k the key whose associated value is to be returned
Returns
  • the value to which the specified key is mapped, or null if this map contains no mapping for the key

public Set<String> keySet ()

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

This implementation returns a set that subclasses AbstractSet. The subclass's iterator method returns a "wrapper object" over this map's entrySet() iterator. The size method delegates to this map's size method and the contains method delegates to this map's containsKey method.

The set is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same set.

Returns
  • a set view of the keys contained in this map

public V put (String k, V v)

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

This implementation always throws an UnsupportedOperationException.

Parameters
k key with which the specified value is to be associated
v value to be associated with the specified key
Returns
  • the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
Throws
UnsupportedOperationException If the given key is not part of this map's initial key set

Protected Methods

protected abstract void init (Object[] ht)

Initializes this map.

This method must construct the map's hash chains and store them into the appropriate elements of the given hash-table row array.