Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util
Class HashIntMap  view HashIntMap download HashIntMap.java

java.lang.Object
  extended byedu.emory.mathcs.util.AbstractIntMap
      extended byedu.emory.mathcs.util.HashIntMap
All Implemented Interfaces:
java.lang.Cloneable, IntMap, java.io.Serializable

public class HashIntMap
extends AbstractIntMap
implements IntMap, java.lang.Cloneable, java.io.Serializable

Hash table based implementation of the IntMap interface. This implementation provides all of the optional map operations, and permixts null values. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the intial capacity too high (or the load factor too low) if iteration performance is important.

An instance of HashIntMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

If many mappings are to be stored in a HashIntMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

 Map m = Collections.synchronizedMap(new HashMap(...));
 

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-determixnistic behavior at an undetermined time in the future.

Since:
1.2
Version:
1.0

Nested Class Summary
private static class HashIntMap.EmptyHashIterator
           
private static class HashIntMap.Entry
          HashMap collision list entry.
private  class HashIntMap.HashIterator
           
 
Field Summary
private  int count
          The total number of mappings in the hash table.
private static HashIntMap.EmptyHashIterator emptyHashIterator
           
private static int ENTRIES
           
private  java.util.Set entrySet
           
private  float loadFactor
          The load factor for the hashtable.
private  int modCount
          The number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash).
private  HashIntMap.Entry[] table
          The hash table data.
private  int threshold
          The table is rehashed when its size exceeds this threshold.
private  java.util.Collection values
           
private static int VALUES
           
 
Constructor Summary
HashIntMap()
          Constructs a new, empty map with a default capacity and load factor, which is 0.75.
HashIntMap(int initialCapacity)
          Constructs a new, empty map with the specified initial capacity and default load factor, which is 0.75.
HashIntMap(int initialCapacity, float loadFactor)
          Constructs a new, empty map with the specified initial capacity and the specified load factor.
HashIntMap(java.util.Map t)
          Constructs a new map with the same mappings as the given map.
 
Method Summary
(package private)  int capacity()
           
 void clear()
          Removes all mappings from this map.
 java.lang.Object clone()
          Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
 boolean containsKey(int key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to the specified value.
 java.util.Set entrySet()
          Returns a collection view of the mappings contained in this map.
 java.lang.Object get(int key)
          Returns the value to which this map maps the specified key.
private  java.util.Iterator getHashIterator(int type)
           
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
(package private)  float loadFactor()
           
 java.lang.Object put(int key, java.lang.Object value)
          Associates the specified value with the specified key in this map.
 void putAll(IntMap t)
          Copies all of the mappings from the specified map to this one.
private  void readObject(java.io.ObjectInputStream s)
          Reconstitute the HashMap instance from a stream (i.e., deserialize it).
private  void rehash()
          Rehashes the contents of this map into a new HashMap instance with a larger capacity.
 java.lang.Object remove(int key)
          Removes the mapping for this key from this map if present.
 int size()
          Returns the number of key-value mappings in this map.
 java.util.Collection values()
          Returns a collection view of the values contained in this map.
private  void writeObject(java.io.ObjectOutputStream s)
          Save the state of the HashIntMap instance to a stream (i.e., serialize it).
 
Methods inherited from class edu.emory.mathcs.util.AbstractIntMap
equals, hashCode, putAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.emory.mathcs.util.IntMap
equals, hashCode
 

Field Detail

table

private transient HashIntMap.Entry[] table
The hash table data.


count

private transient int count
The total number of mappings in the hash table.


threshold

private int threshold
The table is rehashed when its size exceeds this threshold. (The value of this field is (int)(capacity * loadFactor).)


loadFactor

private float loadFactor
The load factor for the hashtable.


modCount

private transient int modCount
The number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash). This field is used to make iterators on Collection-views of the HashMap fail-fast. (See ConcurrentModificationException).


entrySet

private transient java.util.Set entrySet

values

private transient java.util.Collection values

VALUES

private static final int VALUES
See Also:
Constant Field Values

ENTRIES

private static final int ENTRIES
See Also:
Constant Field Values

emptyHashIterator

private static HashIntMap.EmptyHashIterator emptyHashIterator
Constructor Detail

HashIntMap

public HashIntMap(int initialCapacity,
                  float loadFactor)
Constructs a new, empty map with the specified initial capacity and the specified load factor.


HashIntMap

public HashIntMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and default load factor, which is 0.75.


HashIntMap

public HashIntMap()
Constructs a new, empty map with a default capacity and load factor, which is 0.75.


HashIntMap

public HashIntMap(java.util.Map t)
Constructs a new map with the same mappings as the given map. The map is created with a capacity of twice the number of mappings in the given map or 11 (whichever is greater), and a default load factor, which is 0.75.

Method Detail

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface IntMap
Overrides:
size in class AbstractIntMap

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface IntMap
Overrides:
isEmpty in class AbstractIntMap

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value.

Specified by:
containsValue in interface IntMap
Overrides:
containsValue in class AbstractIntMap

containsKey

public boolean containsKey(int key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface IntMap
Overrides:
containsKey in class AbstractIntMap

get

public java.lang.Object get(int key)
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. 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.

Specified by:
get in interface IntMap
Overrides:
get in class AbstractIntMap

rehash

private void rehash()
Rehashes the contents of this map into a new HashMap instance with a larger capacity. This method is called automatically when the number of keys in this map exceeds its capacity and load factor.


put

public java.lang.Object put(int key,
                            java.lang.Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface IntMap
Overrides:
put in class AbstractIntMap

remove

public java.lang.Object remove(int key)
Removes the mapping for this key from this map if present.

Specified by:
remove in interface IntMap
Overrides:
remove in class AbstractIntMap

putAll

public void putAll(IntMap t)
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified Map.

Specified by:
putAll in interface IntMap

clear

public void clear()
Removes all mappings from this map.

Specified by:
clear in interface IntMap
Overrides:
clear in class AbstractIntMap

clone

public java.lang.Object clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.


values

public java.util.Collection values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface IntMap
Overrides:
values in class AbstractIntMap

entrySet

public java.util.Set entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface IntMap
Specified by:
entrySet in class AbstractIntMap

getHashIterator

private java.util.Iterator getHashIterator(int type)

writeObject

private void writeObject(java.io.ObjectOutputStream s)
                  throws java.io.IOException
Save the state of the HashIntMap instance to a stream (i.e., serialize it).


readObject

private void readObject(java.io.ObjectInputStream s)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Reconstitute the HashMap instance from a stream (i.e., deserialize it).


capacity

int capacity()

loadFactor

float loadFactor()