Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class HashMap

java.lang.Object
|
+--java.util.AbstractMap
   |
   +--java.util.HashMap

All Implemented Interfaces:

Map, Cloneable, Serializable, Map


public class HashMap

extends AbstractMap

implements Map, Cloneable, Serializable

This class provides a hashtable-backed implementation of the Map interface.

It uses a hash-bucket approach; that is, hash collisions are handled by linking the new node off of the pre-existing node (or list of nodes). In this manner, techniques such as linear probing (which can cause primary clustering) and rehashing (which does not fit very well with Java's method of precomputing hash codes) are avoided.

Under ideal circumstances (no collisions), HashMap offers O(1) performance on most operations (containsValue() is, of course, O(n)). In the worst case (all keys map to the same hash code -- very unlikely), most operations are O(n).

HashMap is part of the JDK1.2 Collections API. It differs from Hashtable in that it accepts the null key and null values, and it does not support "Enumeration views." Also, it is not synchronized; if you plan to use it in multiple threads, consider using:
Map m = Collections.synchronizedMap(new HashMap(...));

The iterators are fail-fast, meaning that any structural modification, except for remove() called on the iterator itself, cause the iterator to throw a ConcurrentModificationException rather than exhibit non-deterministic behavior.

Since:Authors:See Also:

Constructor Summary

HashMap()

Construct a new HashMap with the default capacity (11) and the default load factor (0.75).
HashMap(java.util.Map m)

Construct a new HashMap from the given Map, with initial capacity the greater of the size of m or the default of 11.
HashMap(int initialCapacity)

Construct a new HashMap with a specific inital capacity and default load factor of 0.75.
HashMap(int initialCapacity, float loadFactor)

Construct a new HashMap with a specific inital capacity and load factor.

Method Summary

voidclear()

Clears the Map so it has no keys.
java.lang.Objectclone()

Returns a shallow clone of this HashMap.
booleancontainsKey(java.lang.Object key)

Returns true if the supplied object equals() a key in this HashMap.
booleancontainsValue(java.lang.Object value)

Returns true if this HashMap contains a value o, such that o.equals(value).
java.util.SetentrySet()

Returns a "set view" of this HashMap's entries.
java.lang.Objectget(java.lang.Object key)

Return the value in this HashMap associated with the supplied key, or null if the key maps to nothing.
booleanisEmpty()

Returns true if there are no key-value mappings currently in this Map.
java.util.SetkeySet()

Returns a "set view" of this HashMap's keys.
java.lang.Objectput(java.lang.Object key, java.lang.Object value)

Puts the supplied value into the Map, mapped by the supplied key.
voidputAll(java.util.Map m)

Copies all elements of the given map into this hashtable.
java.lang.Objectremove(java.lang.Object key)

Removes from the HashMap and returns the value which is mapped by the supplied key.
intsize()

Returns the number of kay-value mappings currently in this Map.
java.util.Collectionvalues()

Returns a "collection view" (or "bag view") of this HashMap's values.

Constructor Details

HashMap

public HashMap()

Construct a new HashMap with the default capacity (11) and the default load factor (0.75).


HashMap

public HashMap(int initialCapacity)

Construct a new HashMap with a specific inital capacity and default load factor of 0.75.

Parameters:

Throws:


HashMap

public HashMap(int initialCapacity, float loadFactor)

Construct a new HashMap with a specific inital capacity and load factor.

Parameters:

Throws:


HashMap

public HashMap(java.util.Map m)

Construct a new HashMap from the given Map, with initial capacity the greater of the size of m or the default of 11.

Every element in Map m will be put into this new HashMap.

Parameters:

Throws:


Method Details

clear

public void clear()

Clears the Map so it has no keys. This is O(1).


clone

public Object clone()

Returns a shallow clone of this HashMap. The Map itself is cloned, but its contents are not. This is O(n).

Returns:


containsKey

public boolean containsKey(java.lang.Object key)

Returns true if the supplied object equals() a key in this HashMap.

Parameters:

Returns:

See Also:


containsValue

public boolean containsValue(java.lang.Object value)

Returns true if this HashMap contains a value o, such that o.equals(value).

Parameters:

Returns:

See Also:


entrySet

public Set entrySet()

Returns a "set view" of this HashMap's entries. The set is backed by the HashMap, so changes in one show up in the other. The set supports element removal, but not element addition.

Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the HashMap in the same sequence.

Returns:

See Also:


get

public Object get(java.lang.Object key)

Return the value in this HashMap associated with the supplied key, or null if the key maps to nothing. NOTE: Since the value could also be null, you must use containsKey to see if this key actually maps to something.

Parameters:

Returns:

See Also:


isEmpty

public boolean isEmpty()

Returns true if there are no key-value mappings currently in this Map.

Returns:


keySet

public Set keySet()

Returns a "set view" of this HashMap's keys. The set is backed by the HashMap, so changes in one show up in the other. The set supports element removal, but not element addition.

Returns:

See Also:


put

public Object put(java.lang.Object key, java.lang.Object value)

Puts the supplied value into the Map, mapped by the supplied key. The value may be retrieved by any object which equals() this key. NOTE: Since the prior value could also be null, you must first use containsKey if you want to see if you are replacing the key's mapping.

Parameters:

Returns:

See Also:


putAll

public void putAll(java.util.Map m)

Copies all elements of the given map into this hashtable. If this table already has a mapping for a key, the new mapping replaces the current one.

Parameters:


remove

public Object remove(java.lang.Object key)

Removes from the HashMap and returns the value which is mapped by the supplied key. If the key maps to nothing, then the HashMap remains unchanged, and null is returned. NOTE: Since the value could also be null, you must use containsKey to see if you are actually removing a mapping.

Parameters:

Returns:


size

public int size()

Returns the number of kay-value mappings currently in this Map.

Returns:


values

public Collection values()

Returns a "collection view" (or "bag view") of this HashMap's values. The collection is backed by the HashMap, so changes in one show up in the other. The collection supports element removal, but not element addition.

Returns:

See Also: