# Class goog.structs.Map.`<K, V>`

code »

Class for Hash Map datastructure.

## Constructor

goog.structs.Map ( opt_map, var_args )

Parameters

opt_map: `*=`
  Map or Object to initialize the map with.
var_args: `...*`
  If 2 or more arguments are present then they will be used as key-value pairs.

Show:PublicProtectedPrivate

## Instance Methods

code » __iterator__ ( opt_keys ) ⇒ `!goog.iter.Iterator`

Returns an iterator that iterates over the values or the keys in the map. This throws an exception if the map was mutated since the iterator was created.

Parameters

opt_keys: `boolean=`
  True to iterate over the keys. False to iterate over the values. The default value is false.

Returns

An iterator over the values or keys in the map.

code » addAll ( map )

Adds multiple key-value pairs from another goog.structs.Map or Object.

Parameters

map: `Object`
  Object containing the data to add.

code » cleanupKeysArray_ ( )

Cleans up the temp keys array by removing entries that are no longer in the map.

code » clear ( )

Removes all key-value pairs from the map.

code » clone ( ) ⇒ `!goog.structs.Map`

Clones a map and returns a new map.

Returns

A new map with the same key-value pairs.

code » containsKey ( key ) ⇒ `boolean`

Whether the map contains the given key.

Parameters

key: `*`
  The key to check for.

Returns

Whether the map contains the key.

code » containsValue ( val ) ⇒ `boolean`

Whether the map contains the given value. This is O(n).

Parameters

val: `V`
  The value to check for.

Returns

Whether the map contains the value.

code » equals ( otherMap, opt_equalityFn ) ⇒ `boolean`

Whether this map is equal to the argument map.

Parameters

otherMap: `goog.structs.Map`
  The map against which to test equality.
opt_equalityFn: `function(V, V): boolean=`
  Optional equality function to test equality of values. If not specified, this will test whether the values contained in each map are identical objects.

Returns

Whether the maps are equal.

code » `<T>` forEach ( f, opt_obj )

Calls the given function on each entry in the map.

Parameters

f
opt_obj: `T=`
  The value of "this" inside f.

code » `<DEFAULT>` get ( key, opt_val ) ⇒ `(V|DEFAULT)`

Returns the value for the given key. If the key is not found and the default value is not given this will return `undefined`.

Parameters

key: `*`
  The key to get the value for.
opt_val: `DEFAULT=`
  The value to return if no item is found for the given key, defaults to undefined.

Returns

The value for the given key.

code » getCount ( ) ⇒ `number`

Returns

The number of key-value pairs in the map.

code » getKeyIterator ( ) ⇒ `!goog.iter.Iterator`

Returns an iterator that iterates over the keys in the map. Removal of keys while iterating might have undesired side effects.

Returns

An iterator over the keys in the map.

code » getKeys ( ) ⇒ `!Array.<string>`

Returns the keys of the map.

Returns

Array of string values.

code » getValueIterator ( ) ⇒ `!goog.iter.Iterator`

Returns an iterator that iterates over the values in the map. Removal of keys while iterating might have undesired side effects.

Returns

An iterator over the values in the map.

code » getValues ( ) ⇒ `!Array.<V>`

Returns the values of the map.

Returns

The values in the map.

code » isEmpty ( ) ⇒ `boolean`

Returns

Whether the map is empty.

code » remove ( key ) ⇒ `boolean`

Removes a key-value pair based on the key. This is O(logN) amortized due to updating the keys array whenever the count becomes half the size of the keys in the keys array.

Parameters

key: `*`
  The key to remove.

Returns

Whether object was removed.

code » set ( key, value ) ⇒ `*`

Adds a key-value pair to the map.

Parameters

key: `*`
  The key.
value: `V`
  The value to add.

Returns

Some subclasses return a value.

code » toObject ( ) ⇒ `!Object`

Returns

Object representation of the map.

code » transpose ( ) ⇒ `!goog.structs.Map`

Returns a new map in which all the keys and values are interchanged (keys become values and values become keys). If multiple keys map to the same value, the chosen transposed value is implementation-dependent. It acts very similarly to {goog.object.transpose(Object)}.

Returns

The transposed map.

## Instance Properties

code » count_ : `number`

The number of key value pairs in the map.

code » keys_ : `!Array.<string>`

An array of keys. This is necessary for two reasons: 1. Iterating the keys using for (var key in this.map_) allocates an object for every key in IE which is really bad for IE6 GC perf. 2. Without a side data structure, we would need to escape all the keys as that would be the only way we could tell during iteration if the key was an internal key or a property of the object. This array can contain deleted keys so it's necessary to check the map as well to see if the key is still in the map (this doesn't require a memory allocation in IE).

code » map_ : `!Object`

Underlying JS object used to implement the map.

code » version_ : `number`

Version used to detect changes while iterating.

## Static Functions

code » goog.structs.Map.defaultEquals ( a, b ) ⇒ `boolean`

Default equality test for values.

Parameters

a: `*`
  The first value.
b: `*`
  The second value.

Returns

Whether a and b reference the same object.

code » goog.structs.Map.hasKey_ ( obj, key ) ⇒ `boolean`

Safe way to test for hasOwnProperty. It even allows testing for 'hasOwnProperty'.

Parameters

obj: `Object`
  The object to test for presence of the given key.
key: `*`
  The key to check for.

Returns

Whether the object has the key.