HashMap

Kind of class:public class
Package:com.bourre.collection
Inherits from:none
Author:Francis Bourre
Classpath:com.bourre.collection.HashMap
File last modified:Monday, 24 November 2008, 11:36:49
An object that maps keys to values. A map cannot contain duplicate keys;
each key can map to at most one value.

The "destructive" methods contained in this interface, that is, the
methods that modify the map on which they operate, are specified to throw

UnsupportedOperationException if this map does not support the
operation. If this is the case, these methods may, but are not required
to, throw an UnsupportedOperationException if the invocation would
have no effect on the map. For example, invoking the putAll(Map)method on an unmodifiable map may, but is not required to, throw the
exception if the map whose mappings are to be "superimposed" is empty.

Some map implementations have restrictions on the keys and values they
may contain. For example, some implementations prohibit null keys and
values, and some have restrictions on the types of their keys. Attempting
to insert an ineligible key or value throws an unchecked exception,
typically NullPointerException or ClassCastException.
Attempting to query the presence of an ineligible key or value may throw an
exception, or it may simply return false; some implementations will exhibit
the former behavior and some will exhibit the latter. More generally,
attempting an operation on an ineligible key or value whose completion
would not result in the insertion of an ineligible element into the map may
throw an exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
interface.

Many methods in Collections Framework interfaces are defined
in terms of the === operator. For example, the
specification for the containsKey( key : Object )method says: "returns true if and
only if this map contains a mapping for a key k such that

key === k." This specification should
not be construed to imply that invoking HashMap.containsKeywith a non-null argument key will cause key === k to
be invoked for any key k. Implementations are free to
implement optimizations whereby the === invocation is avoided,
for example, by first comparing the hash codes of the two keys. (The

HashCodeFactory.getKey(Object) specification guarantees that two
objects with unequal hash codes cannot be equal.) More generally, implementations of
the various Collections Framework interfaces are free to take advantage of
the specified behavior of underlying Object methods wherever the
implementor deems it appropriate.

Summary


Constructor
Instance methods
  • clear : void
    • Removes all of the mappings from this map (optional operation).
  • isEmpty : Boolean
    • Returns true if this map contains no key-value mappings.
  • containsKey (key:*) : Boolean
    • Returns true if this map contains a mapping for the specified
  • containsValue (value:*) : Boolean
    • Returns true if this map maps one or more keys to the
  • put (key:*, value:*) : *
    • Associates the specified value with the specified key in this map
  • get (key:*) : *
    • Returns the value to which the specified key is mapped,
  • remove (key:*) : *
    • Removes the mapping for a key from this map if it is present
  • size : Number
    • Returns the number of key-value mappings in this map.
  • getKeys : Array
    • Returns an Array view of the keys contained in this map.
  • getValues : Array
    • Returns an Array view of the values contained in this map.
  • toString : String
    • Returns the string representation of this instance.

Constructor

HashMap

public function HashMap (
)

Creates a new empty hash map object.

Instance methods

clear

public function clear (
) : void

Removes all of the mappings from this map (optional operation).
The map will be empty after this call returns.

containsKey

public function containsKey (
key:*) : Boolean

Returns true if this map contains a mapping for the specified
key. More formally, returns true if and only if
this map contains a mapping for a key k such that

key === k. (There can be at most one such mapping.)
Parameters:
key:
key object whose presence in this map is to be tested
Returns:
  • true if this map contains a mapping for the specified
    key
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified key is null

containsValue

public function containsValue (
value:*) : Boolean

Returns true if this map maps one or more keys to the
specified value. More formally, returns true if and only if
this map contains at least one mapping to a value v such that

(value === v).
Parameters:
value:
value whose presence in this map is to be tested
Returns:
  • true if this map maps one or more keys to the
    specified value

get

public function get (
key:*) : *

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 === k), then this method returns v; otherwise
it returns null. (There can be at most one such mapping.)

As this map permits null values, a return value of nulldoes 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.

Parameters:
key:
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
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified key is null

getKeys

public function getKeys (
) : Array

Returns an Array view of the keys contained in this map.
Returns:
  • an array view of the keys contained in this map

getValues

public function getValues (
) : Array

Returns an Array view of the values contained in this map.
Returns:
  • an array view of the values contained in this map

isEmpty

public function isEmpty (
) : Boolean

Returns true if this map contains no key-value mappings.
Returns:
  • true if this map contains no key-value mappings

put

public function put (
key:*, value:*) : *

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.)
Parameters:
key :
key with which the specified value is to be associated
value:
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:
  • {VISDOC_LINK_0}NullPointerException — if the specified key or value is null

remove

public function remove (
key:*) : *

Removes the mapping for a key from this map if it is present
(optional operation). More formally, if this map contains a mapping
from key k to value v such that

(key === k), that mapping is removed.
(The map can contain at most one such mapping.)

Returns the value to which this map previously associated the key,
or null if the map contained no mapping for the key.

As this map permits null values, then a return value of

null does not necessarily indicate that the map
contained no mapping for the key; it's also possible that the map
explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the
call returns.

Parameters:
key:
key whose mapping is to be removed from the map
Returns:
  • the previous value associated with key, or
    null if there was no mapping for key.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified key is null

size

public function size (
) : Number

Returns the number of key-value mappings in this map.
Returns:
  • the number of key-value mappings in this map

toString

public function toString (
) : String

Returns the string representation of this instance.
Returns:
  • the string representation of this instance