edu.umd.cloud9.util.map
Class HMapIV<V>

java.lang.Object
  extended by edu.umd.cloud9.util.map.HMapIV<V>
All Implemented Interfaces:
MapIV<V>, Serializable, Cloneable
Direct Known Subclasses:
HMapIVW

public class HMapIV<V>
extends Object
implements MapIV<V>, Cloneable, Serializable

Hash-based implementation of the MapIV interface. MapIV is a specialized variant the standard Java Map interface, except that the keys are hard coded as ints for efficiency reasons (values can be arbitrary objects). This implementation was adapted from HashMap version 1.73, 03/13/07. See this benchmark for an efficiency comparison.

See Also:
Serialized Form

Constructor Summary
HMapIV()
          Constructs an empty HMapII with the default initial capacity (1024) and the default load factor (0.75).
HMapIV(int initialCapacity)
          Constructs an empty HMapII with the specified initial capacity and the default load factor (0.75).
HMapIV(int initialCapacity, float loadFactor)
          Constructs an empty HMapII with the specified initial capacity and load factor.
HMapIV(MapIV<V> m)
          Constructs a new HMapII with the same mappings as the specified MapIV.
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 Object clone()
          Returns a shallow copy of this HMapII 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(V value)
          Returns true if this map contains one or more mappings with the specified value.
 Set<MapIV.Entry<V>> entrySet()
          Returns a Set view of the mappings contained in this map.
 V get(int key)
          Returns the value to which the specified key is mapped, or throws NoSuchElementException if this map contains no mapping for the key.
 MapIV.Entry<V>[] getEntriesSortedByValue()
          Returns entries sorted by descending value.
 MapIV.Entry<V>[] getEntriesSortedByValue(int n)
          Returns top n entries sorted by descending value.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 Set<Integer> keySet()
          Returns a Set view of the keys contained in this map.
 void put(int key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(MapIV<V> m)
          Copies all of the mappings from the specified map to this map.
 V remove(int key)
          Removes the mapping for a key from this map if it is present.
 int size()
          Returns the number of key-value mappings in this map.
 String toString()
           
 Collection<V> values()
          Returns a Collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.umd.cloud9.util.map.MapIV
equals, hashCode
 

Constructor Detail

HMapIV

public HMapIV(int initialCapacity,
              float loadFactor)
Constructs an empty HMapII with the specified initial capacity and load factor.

Parameters:
initialCapacity - the initial capacity
loadFactor - the load factor
Throws:
IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive

HMapIV

public HMapIV(int initialCapacity)
Constructs an empty HMapII with the specified initial capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
IllegalArgumentException - if the initial capacity is negative.

HMapIV

public HMapIV()
Constructs an empty HMapII with the default initial capacity (1024) and the default load factor (0.75).


HMapIV

public HMapIV(MapIV<V> m)
Constructs a new HMapII with the same mappings as the specified MapIV. The HMapIV is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified MapIV.

Parameters:
m - the map whose mappings are to be placed in this map
Throws:
NullPointerException - if the specified map is null
Method Detail

size

public int size()
Description copied from interface: MapIV
Returns the number of key-value mappings in this map.

Specified by:
size in interface MapIV<V>
Returns:
the number of key-value mappings in this map

isEmpty

public boolean isEmpty()
Description copied from interface: MapIV
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface MapIV<V>
Returns:
true if this map contains no key-value mappings

get

public V get(int key)
Description copied from interface: MapIV
Returns the value to which the specified key is mapped, or throws NoSuchElementException if this map contains no mapping for the key.

Specified by:
get in interface MapIV<V>
Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which the specified key is mapped

containsKey

public boolean containsKey(int key)
Description copied from interface: MapIV
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface MapIV<V>
Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key

put

public void put(int key,
                V value)
Description copied from interface: MapIV
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value.

Specified by:
put in interface MapIV<V>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key

putAll

public void putAll(MapIV<V> m)
Description copied from interface: MapIV
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface MapIV<V>
Parameters:
m - mappings to be stored in this map

remove

public V remove(int key)
Description copied from interface: MapIV
Removes the mapping for a key from this map if it is present. No action is performed if this map does not contain the key.

Specified by:
remove in interface MapIV<V>
Parameters:
key - key whose mapping is to be removed from the map

clear

public void clear()
Description copied from interface: MapIV
Removes all of the mappings from this map. The map will be empty after this call returns.

Specified by:
clear in interface MapIV<V>

containsValue

public boolean containsValue(V value)
Description copied from interface: MapIV
Returns true if this map contains one or more mappings with the specified value.

Specified by:
containsValue in interface MapIV<V>
Parameters:
value - value whose presence in this map is to be tested
Returns:
true this map contains one or more mappings with the specified value

clone

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

Overrides:
clone in class Object
Returns:
a shallow copy of this map

keySet

public Set<Integer> keySet()
Description copied from interface: MapIV
Returns a Set view of the keys contained in this map. Note that this is a inefficient operation since it triggers autoboxing of the int keys, which is exactly what this implementation is trying to avoid. Unlike a standard Java Map, values in the backing map cannot be altered with this collection view.

Specified by:
keySet in interface MapIV<V>
Returns:
a set view of the keys contained in this map

values

public Collection<V> values()
Description copied from interface: MapIV
Returns a Collection view of the values contained in this map.

Specified by:
values in interface MapIV<V>
Returns:
a collection view of the values contained in this map

entrySet

public Set<MapIV.Entry<V>> entrySet()
Description copied from interface: MapIV
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. See Map.entrySet() for more details.

Specified by:
entrySet in interface MapIV<V>
Returns:
a set view of the mappings contained in this map

toString

public String toString()
Overrides:
toString in class Object

getEntriesSortedByValue

public MapIV.Entry<V>[] getEntriesSortedByValue()
Returns entries sorted by descending value. Ties broken by the key.

Returns:
entries sorted by descending value

getEntriesSortedByValue

public MapIV.Entry<V>[] getEntriesSortedByValue(int n)
Returns top n entries sorted by descending value. Ties broken by the key.

Parameters:
n - number of entries to return
Returns:
top n entries sorted by descending value