de.enough.polish.util
Class HashMap<K,V>

java.lang.Object
  extended by de.enough.polish.util.HashMap<K,V>
Type Parameters:
K - when you use the enough-polish-client-java5.jar you can parameterize the HashMap, e.g. HashMap<User, Message> = new HashMap<User, Message>(10);
V - when you use the enough-polish-client-java5.jar you can parameterize the HashMap, e.g. HashMap<User, Message> = new HashMap<User, Message>(10);
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
Properties

public class HashMap<K,V>
extends Object
implements Map<K,V>

Provides the functionality of the J2SE java.util.HashMap for J2ME applications.

In contrast to the java.util.Hashtable (which is available on J2ME platforms), this implementation is not synchronized and faster.

This implementation uses chains for resolving collisions, that means when a key-value pair has the same hash code as a previous inserted item, the new item is linked to the previous item. Depending on your situation The OpenAddressingHashMap implementation might be better, especially when you do not have many collisions (items with the same hash code).

Copyright (c) Enough Software 2005 - 2009

 history
        30-Nov-2005 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
static int DEFAULT_INITIAL_CAPACITY
          The default capacity is 16, this results in an internal size of 21
static int DEFAULT_LOAD_FACTOR
          The default load factor is 75 (=75%), so the HashMap is increased when 75% of it's capacity is reached
 
Constructor Summary
HashMap()
          Creates a new HashMap with the default initial capacity 16 and a load factor of 75%.
HashMap(int initialCapacity)
          Creates a new HashMap with the specified initial capacity.
HashMap(int initialCapacity, int loadFactor)
          Creates a new HashMap with the specified initial capacity and the specified load factor.
 
Method Summary
 void clear()
          Removes all elements from this map.
 boolean containsKey(K key)
          Checks if a value has been stored in this map.
 boolean containsValue(V value)
          Checks the given value has been stored in this map.
 V get(K key)
          Gets the value that has been stored for the specified key.
 boolean isEmpty()
          Determines whether this map is empty.
 Object[] keys()
          Retrieves all keys that have been stored in this map.
 K[] keys(K[] objects)
          Retrieves all keys that have been stored in this map.
 Iterator keysIterator()
          Iterates over the keys of this map.
 V put(K key, V value)
          Adds a key-value pair to this map.
 V remove(K key)
          Removes the key-value pair from this map.
 int size()
          The size of this map.
 String toString()
          Returns String containing the String representations of all objects of this map.
 Object[] values()
          Retrieves all values that have been stored in this map.
 V[] values(V[] objects)
          Retrieves all values that have been stored in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_INITIAL_CAPACITY

public static final int DEFAULT_INITIAL_CAPACITY
The default capacity is 16, this results in an internal size of 21

See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

public static final int DEFAULT_LOAD_FACTOR
The default load factor is 75 (=75%), so the HashMap is increased when 75% of it's capacity is reached

See Also:
Constant Field Values
Constructor Detail

HashMap

public HashMap()
Creates a new HashMap with the default initial capacity 16 and a load factor of 75%.


HashMap

public HashMap(int initialCapacity)
Creates a new HashMap with the specified initial capacity.

Parameters:
initialCapacity - the initial number of elements that this map can hold without needing to increase it's internal size. Must not be 0.

HashMap

public HashMap(int initialCapacity,
               int loadFactor)
Creates a new HashMap with the specified initial capacity and the specified load factor.

Parameters:
initialCapacity - the initial number of elements that this map can hold without needing to increase it's internal size.
loadFactor - the loadfactor in percent, a number between 0 and 100. When the loadfactor is 100, the size of this map is only increased after all slots have been filled.
Throws:
IllegalArgumentException - when the initialCapacity is less than 1
Method Detail

put

public V put(K key,
             V value)
Description copied from interface: Map
Adds a key-value pair to this map.

Specified by:
put in interface Map<K,V>
Parameters:
key - the key
value - the value
Returns:
the value that has been stored previously for the given key, or null.

get

public V get(K key)
Description copied from interface: Map
Gets the value that has been stored for the specified key.

Specified by:
get in interface Map<K,V>
Parameters:
key - the key
Returns:
the value or null, when for the given key no value has been stored.

remove

public V remove(K key)
Description copied from interface: Map
Removes the key-value pair from this map.

Specified by:
remove in interface Map<K,V>
Parameters:
key - the key
Returns:
the value or null, when for the given key no value has been stored.

isEmpty

public boolean isEmpty()
Description copied from interface: Map
Determines whether this map is empty. This is equivalent to calling map.getSize() == 0.

Specified by:
isEmpty in interface Map<K,V>
Returns:
true when this map is empty.

size

public int size()
Description copied from interface: Map
The size of this map.

Specified by:
size in interface Map<K,V>
Returns:
the size of this map, 0 when no entries have been added yet.

containsKey

public boolean containsKey(K key)
Description copied from interface: Map
Checks if a value has been stored in this map. This is equivalent to calling map.get( key ) != null.

Specified by:
containsKey in interface Map<K,V>
Parameters:
key - the key
Returns:
true when a value has been stored in this map to the specified key.

containsValue

public boolean containsValue(V value)
Description copied from interface: Map
Checks the given value has been stored in this map. This is quite an expensive operation, since all elements need to be checked in the worst case.

Specified by:
containsValue in interface Map<K,V>
Parameters:
value - the value
Returns:
true when the value has been stored in this map.

clear

public void clear()
Description copied from interface: Map
Removes all elements from this map.

Specified by:
clear in interface Map<K,V>

values

public Object[] values()
Description copied from interface: Map
Retrieves all values that have been stored in this map.

Specified by:
values in interface Map<K,V>
Returns:
an object array with all values.

values

public V[] values(V[] objects)
Description copied from interface: Map
Retrieves all values that have been stored in this map.

Specified by:
values in interface Map<K,V>
Parameters:
objects - the typed array in which the elements are stored
Returns:
an object array with all values.

keys

public Object[] keys()
Description copied from interface: Map
Retrieves all keys that have been stored in this map.

Specified by:
keys in interface Map<K,V>
Returns:
an object array with all keys.

keys

public K[] keys(K[] objects)
Description copied from interface: Map
Retrieves all keys that have been stored in this map.

Specified by:
keys in interface Map<K,V>
Parameters:
objects - the typed array in which the keys are stored
Returns:
an object array with all keys.

toString

public String toString()
Returns String containing the String representations of all objects of this map.

Overrides:
toString in class Object
Returns:
the stored elements in a String representation.

keysIterator

public Iterator keysIterator()
Description copied from interface: Map
Iterates over the keys of this map. In contrast to using the keys() method no additional array is created. When the map is modified while the iterator is being used, the behavior of the iterator is unspecified (unless the iterator.remove() method is used).

Specified by:
keysIterator in interface Map<K,V>
Returns:
an iterator that contains all keys.