Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HashMap<K, V>

A dictionary, mapping keys to values.

Type parameters

  • K

    the key type

  • V

    the value type

Hierarchy

  • HashMap

Implements

Index

Methods

Static empty

  • The empty map.

    Type parameters

    • K

      the key type

    • V

      the value type

    Returns HashMap<K, V>

Static isEmpty

  • isEmpty<K, V>(v: HashMap<K, V>): boolean
  • Curried predicate to find out whether the HashMap is empty.

    Vector.of(HashMap.of([1,2]), HashMap.empty<number,number>())
        .filter(HashMap.isEmpty)
    => Vector.of(HashMap.empty<number,number>())
    

    Type parameters

    • K

    • V

    Parameters

    Returns boolean

Static isNotEmpty

  • isNotEmpty<K, V>(v: HashMap<K, V>): boolean
  • Curried predicate to find out whether the HashMap is empty.

    Vector.of(HashMap.of([1,2]), HashMap.empty<number,number>())
        .filter(HashMap.isNotEmpty)
    => Vector.of(HashMap.of([1,2]))
    

    Type parameters

    • K

    • V

    Parameters

    Returns boolean

Static of

  • Build a HashMap from key-value pairs.

    HashMap.of([1,"a"],[2,"b"])
    

    Type parameters

    • K

    • V

    Parameters

    Returns HashMap<K, V>

Static ofIterable

  • Build a HashMap from an iterable containing key-value pairs.

    HashMap.ofIterable(Vector.of<[number,string]>([1,"a"],[2,"b"]));

    Type parameters

    • K

    • V

    Parameters

    Returns HashMap<K, V>

Static ofObjectDictionary

  • ofObjectDictionary<V>(object: object): HashMap<string, V>
  • Build a HashMap from a javascript object literal representing a dictionary. Note that the key type must always be string, as that's the way it works in javascript. Also note that entries with undefined values will be stripped from the map.

    HashMap.ofObjectDictionary<number>({a:1,b:2})
    => HashMap.of(["a",1],["b",2])
    

    Type parameters

    • V

    Parameters

    • object: object
      • [index: string]: V | undefined

    Returns HashMap<string, V>

__@iterator

  • __@iterator(): Iterator<[K, V]>

__computed

  • __computed(): string

allMatch

  • allMatch(predicate: function): boolean
  • Returns true if the predicate returns true for all the elements in the collection.

    Parameters

    • predicate: function
        • (k: K, v: V): boolean
        • Parameters

          • k: K
          • v: V

          Returns boolean

    Returns boolean

anyMatch

  • anyMatch(predicate: function): boolean
  • Returns true if there the predicate returns true for any element in the collection.

    Parameters

    • predicate: function
        • (k: K, v: V): boolean
        • Parameters

          • k: K
          • v: V

          Returns boolean

    Returns boolean

contains

containsKey

  • Returns true if there is item with that key in the collection, false otherwise.

    HashMap.of<number,string>([1,"a"],[2,"b"]).containsKey(1);
    => true
    
    HashMap.of<number,string>([1,"a"],[2,"b"]).containsKey(3);
    => false
    

    Parameters

    Returns boolean

equals

  • Two objects are equal if they represent the same value, regardless of whether they are the same object physically in memory.

    Parameters

    Returns boolean

filter

  • filter(predicate: function): HashMap<K, V>
  • Call a predicate for each element in the collection, build a new collection holding only the elements for which the predicate returned true.

    Parameters

    • predicate: function
        • (k: K, v: V): boolean
        • Parameters

          • k: K
          • v: V

          Returns boolean

    Returns HashMap<K, V>

filterKeys

  • filterKeys<U>(fn: function): HashMap<U, V>
  • filterKeys(predicate: function): HashMap<K, V>
  • Call a predicate for each key in the collection, build a new collection holding only the elements for which the predicate returned true.

    HashMap.of<number,string>([1,"a"],[2,"b"]).filterKeys(k=>k%2===0)
    => HashMap.of<number,string>([2,"b"])
    

    Type parameters

    • U: K

    Parameters

    • fn: function
        • (v: K): boolean
        • Parameters

          • v: K

          Returns boolean

    Returns HashMap<U, V>

  • Parameters

    • predicate: function
        • (k: K): boolean
        • Parameters

          • k: K

          Returns boolean

    Returns HashMap<K, V>

filterValues

  • filterValues<U>(fn: function): HashMap<K, U>
  • filterValues(predicate: function): HashMap<K, V>
  • Call a predicate for each value in the collection, build a new collection holding only the elements for which the predicate returned true.

    HashMap.of<number,string>([1,"a"],[2,"ab"]).filterValues(v=>v.length>1)
    => HashMap.of<number,string>([2,"ab"])
    

    Type parameters

    • U: V

    Parameters

    • fn: function
        • (v: V): boolean
        • Parameters

          • v: V

          Returns boolean

    Returns HashMap<K, U>

  • Parameters

    • predicate: function
        • (k: V): boolean
        • Parameters

          • k: V

          Returns boolean

    Returns HashMap<K, V>

findAny

  • findAny(predicate: function): Option<[K, V]>
  • Search for an item matching the predicate you pass, return Option.Some of that element if found, Option.None otherwise. We name the method findAny instead of find to emphasize that there is not ordering in a hashset.

    HashMap.of<number,string>([1,'a'],[2,'b'],[3,'c'])
        .findAny((k,v) => k>=2 && v === "c")
    => Option.of([3,'c'])
    
    HashMap.of<number,string>([1,'a'],[2,'b'],[3,'c'])
        .findAny((k,v) => k>=3 && v === "b")
    => Option.none<[number,string]>()
    

    Parameters

    • predicate: function
        • (k: K, v: V): boolean
        • Parameters

          • k: K
          • v: V

          Returns boolean

    Returns Option<[K, V]>

flatMap

  • flatMap<K2, V2>(fn: function): HashMap<K2, V2>
  • Calls the function you give for each item in the map, your function returns a map, all the maps are merged.

    Type parameters

    • K2

    • V2

    Parameters

    • fn: function

    Returns HashMap<K2, V2>

fold

  • fold(zero: [K, V], fn: function): [K, V]
  • Reduces the collection to a single value using the associative binary function you give. Since the function is associative, order of application doesn't matter.

    Example:

    HashMap.of<number,string>([1,"a"],[2,"b"],[3,"c"])
     .fold([0,""], ([a,b],[c,d])=>[a+c, b>d?b:d])
    => [6,"c"]
    

    Parameters

    • zero: [K, V]
    • fn: function
        • (v1: [K, V], v2: [K, V]): [K, V]
        • Parameters

          • v1: [K, V]
          • v2: [K, V]

          Returns [K, V]

    Returns [K, V]

foldLeft

  • foldLeft<U>(zero: U, fn: function): U
  • Reduces the collection to a single value. Left-associative. No guarantees for the order of items in a hashset!

    Example:

    HashMap.of([1,"a"], [2,"bb"], [3,"ccc"])
    .foldLeft(0, (soFar,[item,val])=>soFar+val.length);
    => 6
    

    Type parameters

    • U

    Parameters

    • zero: U

      The initial value

    • fn: function

      A function taking the previous value and the current collection item, and returning an updated value.

        • (soFar: U, cur: [K, V]): U
        • Parameters

          • soFar: U
          • cur: [K, V]

          Returns U

    Returns U

foldRight

  • foldRight<U>(zero: U, fn: function): U
  • Reduces the collection to a single value. Right-associative. No guarantees for the order of items in a hashset!

    Example:

    HashMap.of([1,"a"], [2,"bb"], [3,"ccc"])
    .foldRight(0, ([item,value],soFar)=>soFar+value.length);
    => 6
    

    Type parameters

    • U

    Parameters

    • zero: U

      The initial value

    • fn: function

      A function taking the current collection item and the previous value , and returning an updated value.

        • (cur: [K, V], soFar: U): U
        • Parameters

          • cur: [K, V]
          • soFar: U

          Returns U

    Returns U

forEach

  • forEach(fun: function): HashMap<K, V>
  • Call a function for element in the collection.

    Parameters

    • fun: function
        • (x: [K, V]): void
        • Parameters

          • x: [K, V]

          Returns void

    Returns HashMap<K, V>

get

hashCode

  • hashCode(): number
  • Get a number for that object. Two different values may get the same number, but one value must always get the same number. The formula can impact performance.

    Returns number

isEmpty

  • isEmpty(): boolean

keySet

length

  • length(): number

map

  • map<K2, V2>(fn: function): HashMap<K2, V2>

mapValues

  • mapValues<V2>(fn: function): HashMap<K, V2>
  • Return a new map where keys are the same as in this one, but values are transformed by the mapper function you give. You return key,value as pairs.

    Type parameters

    • V2

    Parameters

    • fn: function
        • (v: V): V2
        • Parameters

          • v: V

          Returns V2

    Returns HashMap<K, V2>

mergeWith

  • Create a new map combining the entries of this map, and the other map you give. In case an entry from this map and the other map have the same key, the merge function will be invoked to get a combined value.

    It is guaranteed that the merge function first parameter will be the entry from this map, and the second parameter from the map you give.

    Parameters

    • elts: Iterable<[K & WithEquality, V]>
    • merge: function

      a merge function to combine two values in case two entries share the same key.

        • (v1: V, v2: V): V
        • Parameters

          • v1: V
          • v2: V

          Returns V

    Returns HashMap<K, V>

put

  • Add a new entry in the map. If there was entry with the same key, it will be overwritten.

    Parameters

    Returns HashMap<K, V>

putWithMerge

  • Add a new entry in the map; in case there was already an entry with the same key, the merge function will be invoked with the old and the new value to produce the value to take into account.

    It is guaranteed that the merge function first parameter will be the entry from this map, and the second parameter from the map you give.

    Parameters

    • k: K & WithEquality

      the key

    • v: V

      the value

    • merge: function

      a function to merge old and new values in case of conflict.

        • (v1: V, v2: V): V
        • Parameters

          • v1: V
          • v2: V

          Returns V

    Returns HashMap<K, V>

reduce

  • reduce(combine: function): Option<[K, V]>
  • Reduces the collection to a single value by repeatedly calling the combine function. No starting value. The order in which the elements are passed to the combining function is undetermined.

    Parameters

    • combine: function
        • (v1: [K, V], v2: [K, V]): [K, V]
        • Parameters

          • v1: [K, V]
          • v2: [K, V]

          Returns [K, V]

    Returns Option<[K, V]>

remove

single

  • If the collection contains a single element, return Some of its value, otherwise return None.

    Returns Option<[K, V]>

toArray

  • toArray(): Array<[K, V]>

toJsMap

  • toJsMap(keyConvert: function): Map<string, V>
  • toJsMap(keyConvert: function): Map<number, V>
  • toJsMap(keyConvert: function): Map<boolean, V>

toLinkedList

toObjectDictionary

  • toObjectDictionary(keyConvert: function): object
  • Convert to a javascript object dictionary You must provide a function to convert the key to a string.

    HashMap.of<string,number>(["a",1],["b",2])
        .toObjectDictionary(x=>x);
    => {a:1,b:2}
    

    Parameters

    • keyConvert: function
        • (k: K): string
        • Parameters

          • k: K

          Returns string

    Returns object

    • [index: string]: V

toString

  • toString(): string

toVector

  • Convert this map to a vector of key,value pairs. Note that Map is already an iterable of key,value pairs!

    Returns Vector<[K, V]>

transform

  • transform<U>(converter: function): U
  • Transform this value to another value type. Enables fluent-style programming by chaining calls.

    Type parameters

    • U

    Parameters

    • converter: function

    Returns U

valueIterable

  • valueIterable(): Iterable<V>
  • Get an iterable containing all the values in the map (can't return a set as we don't constrain map values to have equality in the generics type)

    Returns Iterable<V>

Generated using TypeDoc