new HashMap(opt_map, var_args)
This file contains an implementation of a Hash Map structure. It is based on goog.structs.Map from the google closure library, but has been modified to accept any kind of objects as keys (not only Strings or Numbers). Hence, the functionality is similar to that of java.util.HashMap. WARNING: keys will be modified, a property named something like "js_cols_uid_2kczq5" will be added. This liberates the user of implementing a getHashCode function, and improves performance as hashing collisions are avoided. The assymptotic running time for important operations are below:
Method big-O ---------------------------------------------------------------------------- - clear O(1) - clone O(n logn) - contains O(1) - containsAll O(m) m is the cardinality of the supplied collection - every O(n * O(f)) f is the function supplied as argument - filter O(n * O(f)) f is the function supplied as argument - forEach O(n * O(f)) f is the function supplied as argument - get O(1) - getValues O(n) - insert O(1) - insertAll O(m) m is the cardinality of the supplied collection - map O(n * O(f)) f is the function supplied as argument - remove O(1) - removeAll O(m logn) m is the cardinality of the supplied collection - some O(n * O(f)) f is the function supplied as argument - contains O(n * O(f)) f is the function supplied as argument
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
opt_map |
* |
<optional> |
Map or Object to initialize the map with. |
var_args |
* |
<repeatable> |
If 2 or more arguments are present then they will be used as key-value pairs. |
Methods
-
clear()
-
Removes all key-value pairs from the map.
-
clone() → {js_cols.HashMap}
-
Clones a map and returns a new map.
Returns:
A new map with the same key-value pairs.
- Type
- js_cols.HashMap
-
contains(key) → {boolean}
-
This operation is identical to containsKey. Whether the map contains the given key.
Parameters:
Name Type Description key
* The key to check for.
Returns:
Whether the map contains the key.
- Type
- boolean
-
containsAll(col) → {Boolean}
-
Checks that all keys contained in the collection are also contained as keys in the map. If the collection has no notion of keys (i.e. an Array or a Set), the values of the collection will be interpreted as keys in this map.
Parameters:
Name Type Description col
Returns:
- Type
- Boolean
-
containsKey(key) → {boolean}
-
Whether the map contains the given key.
Parameters:
Name Type Description key
* The key to check for.
Returns:
Whether the map contains the key.
- Type
- boolean
-
containsValue(val) → {boolean}
-
Whether the map contains the given value. This is O(n).
Parameters:
Name Type Description val
* The value to check for.
Returns:
Whether the map contains the value.
- Type
- boolean
-
defaultEquals(a, b) → {boolean}
-
Default equality test for values.
Parameters:
Name Type Description a
* The first value.
b
* The second value.
Returns:
Whether a and b reference the same object.
- Type
- boolean
-
equals(otherMap, opt_equalityFn) → {boolean}
-
Whether this map is equal to the argument map.
Parameters:
Name Type Argument Description otherMap
js_cols.HashMap The map against which to test equality.
opt_equalityFn
function <optional>
Optional comparison function. Should take 2 arguments to compare, and return true if the arguments are equal. Defaults to js_cols.HashMap.defaultEquals which compares the elements using the built-in '===' operator.
Returns:
Whether the maps are equal.
- Type
- boolean
-
every(f, opt_obj) → {boolean}
-
Calls a function on each item in the HashMap and returns true only if every function call returns a true-like value.
Parameters:
Name Type Argument Description f
function The function to call for each key/value pair. The function takes three arguments: the value, the key, and the HashMap, and returns a boolean.
opt_obj
Object <optional>
The object context to use as "this" for the function.
Returns:
Whether f evaluates to true for every item in the HashMap.
- Type
- boolean
-
filter(f, opt_obj) → {js_cols.HashMap}
-
Calls a function on each key/value pair in the HashMap, if the function returns true, the key/value pair is inserted into a new HashMap that is returned when the map is fully traversed
Parameters:
Name Type Argument Description f
function The function to call for each item. The function takes three arguments: the value, the key, and the HashMap.
opt_obj
Object <optional>
The object context to use as "this" for the function.
Returns:
a new map with the key / value pairs that evaluated to true in the function calls
- Type
- js_cols.HashMap
-
forEach(f, opt_obj)
-
Calls a function on each key/value pair in the HashMap.
Parameters:
Name Type Argument Description f
function The function to call for each key/value pair. The function takes three arguments: the value, the key, and the HashMap.
opt_obj
Object <optional>
The object context to use as "this" for the function.
-
get(key, opt_val) → {*}
-
Returns the value for the given key. If the key is not found and the default value is not given this will return {@code undefined}.
Parameters:
Name Type Argument Description key
* The key to get the value for.
opt_val
* <optional>
The value to return if no value is found for the given key, defaults to undefined.
Returns:
The value for the given key.
- Type
- *
-
getCount() → {number}
-
Returns:
The number of key-value pairs in the map.
- Type
- number
-
getKeys() → {Array}
-
Returns the keys of the map.
Returns:
Array of key contained in this map.
- Type
- Array
-
getValues() → {Array}
-
Returns the values of the map.
Returns:
The values in the map.
- Type
- Array
-
insert(key, value)
-
Adds a key-value pair to the map.
Parameters:
Name Type Description key
* The key.
value
* The value to insert.
-
insertAll(col)
-
Inserts a collection of key/value pairs into the map If the collection has no notion of keys (i.e. an Array or Set) each element is inserted as both key and value (mapping to it self)
Parameters:
Name Type Description col
-
intersection(col) → {js_cols.HashMap}
-
Finds all key/value pairs that are present in both this map and the given collection. If the collection has no notion of keys (i.e. a Set or an Array), each element of the collection will be treated as key, and it will be inserted to the returned map with its corresponding value from this map. This operation is O(n).
Parameters:
Name Type Description col
Returns:
A new set containing all the key/value pairs (primitives or objects) present in both this set and the given collection.
- Type
- js_cols.HashMap
-
isEmpty() → {boolean}
-
Returns:
Whether the map is empty.
- Type
- boolean
-
isSubmapOf(col) → {Boolean}
-
Detects wheter all key/value pairs present in this map are also present in the given collection. If the collection has no notion of keys (i.e. a Set or an Array), the result will be whether the keys in this map is a subset of the elements in the collection. This operation is O(n).
Parameters:
Name Type Description col
Returns:
wheter this map is a submap of col
- Type
- Boolean
-
map(f, opt_obj) → {Array}
-
Calls a function on each key/value pair in the HashMap and returns the results of those calls in an array.
Parameters:
Name Type Argument Description f
function The function to call for each key/value pair. The function takes three arguments: the value, the key, and the HashMap.
opt_obj
Object <optional>
The object context to use as "this" for the function.
Returns:
The results of the function calls for each key/value pair in the HashMap.
- Type
- Array
-
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:
Name Type Description key
* The key to remove.
Returns:
Whether object was removed.
- Type
- boolean
-
removeAll(col)
-
Removes a collection of key/value pairs into the map If the collection has no notion of keys (i.e. an Array or Set), the values in the collection are treated as keys in the map, and the values associated with those keys are removed.
Parameters:
Name Type Description col
-
some(f, opt_obj) → {boolean}
-
Calls a function on each key/value pair in the HashMap and returns true if any of those function calls returns a true-like value.
Parameters:
Name Type Argument Description f
function The function to call for each key/value pair. The function takes three arguments: the value, the key and the HashMap, and returns a boolean.
opt_obj
Object <optional>
The object context to use as "this" for the function.
Returns:
Whether f evaluates to true for at least one key/value pair in the HashMap.
- Type
- boolean
-
transpose() → {js_cols.HashMap}
-
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.
- Type
- js_cols.HashMap