interface VariantMultiMap.NonEmpty<K,V>
A non-empty type-variant immutable MultiMap of key type K, and value type V. In the Map, each key has at least one value. See the MultiMap documentation and the VariantMultiMap API documentation
Extends: Streamable.NonEmpty<T>
, VariantMultiMap<K,V>
, VariantMultiMapBase.NonEmpty<K,V,Tp>
Type parameters
Name | Description |
---|---|
K | the key type |
V | the value type |
Type-variance means that both the key and value types can be widened in a type-safe way without casting. @note As a consequence of being variant, the type does not contain methods that (can) add new elements to the collection.
Properties
isEmpty
Returns false since this collection is known to be non-empty
isEmpty
keyMap
Returns the non-empty Map representation of this collection.
keyMap
Definition
readonly keyMap:
WithKeyValue
<Tp, K, V>['keyMapNonEmpty'];
const m = HashMultiMapHashValue.of([1, 1], [2, 2])
const map: HashMap.NonEmpty<number, HashSet.NonEmpty<number>> = m.keyMap
Overrides
keySize
Returns the number of keys in the collection.
keySize
size
Returns the number of unique key-value combinations in this collection.
size
Methods
[Symbol.iterator]
Returns a FastIterator
instance used to iterate over the values of this Iterable
.
[Symbol.iterator]
FastIterator
instance used to iterate over the values of this Iterable
.asNormal
Returns this collection typed as a 'possibly empty' collection.
asNormal
Definition
asNormal():
WithKeyValue
<Tp, K, V>['normal'];
HashMultiMapHashValue.of([1, 1], [2, 2]).asNormal(); // type: HashMultiMapHashValue<number, number>
Overrides
assumeNonEmpty
Returns a self reference since this collection is known to be non-empty.
assumeNonEmpty
filter
Returns a collection containing only those entries that satisfy given pred
predicate.
filter
pred
predicate.Definition
filter(pred: (entry: [K, V], index: number, halt: () => void) => boolean, options?: {
negate?: boolean;
}):
WithKeyValue
<Tp, K, V>['normal'];
Parameters
Name | Type | Description |
---|---|---|
pred | (entry: [K, V], index: number, halt: () => void) => boolean | a predicate function receiving: - entry : the next entry- index : the entry index- halt : a function that, when called, ensures no next entries are passed |
options | { negate?: boolean; } | (optional) an object containing the following properties: - negate: (default: false) when true will negate the predicate |
HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c'])
.filter(entry => entry[0] === 2 || entry[1] === 'c')
.toArray()
// => [[2, 'b'], [1, 'c']]
Overrides
forEach
Performs given function f
for each entry of the collection, using given state
as initial traversal state.
forEach
f
for each entry of the collection, using given state
as initial traversal state.Definition
forEach(f: (entry: [K, V], index: number, halt: () => void) => void, options?: {
state?:
TraverseState
;
}): void;
Parameters
Name | Type | Description |
---|---|---|
f | (entry: [K, V], index: number, halt: () => void) => void | the function to perform for each element, receiving: - entry : the next tuple of a key and value- index : the index of the element- halt : a function that, if called, ensures that no new elements are passed |
options | { state?: TraverseState ; } |
HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c']).forEach((entry, i, halt) => {
console.log([entry[1], entry[0]]);
if (i >= 1) halt();
})
// => logs ['a', 1] ['c', 1] (or other order)
O(N)
Overrides
getValues
Returns the value collection associated to the given key
.
getValues
key
.Definition
getValues<UK = K>(key:
RelatedTo
<K, UK>):
WithKeyValue
<Tp, K, V>['keyMapValues'];
Type parameters
Name | Default | Description |
---|---|---|
UK | K |
Parameters
Name | Type | Description |
---|---|---|
key | RelatedTo <K, UK> | the key to look for |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'])
m.getValues(1).toArray() // => ['a']
m.getValues(10).toArray() // => []
Overrides
hasEntry
Returns true if the given key
has the given value
as one of its values in the collection.
hasEntry
key
has the given value
as one of its values in the collection.Definition
hasEntry<UK = K>(key:
RelatedTo
<K, UK>, value: V): boolean;
Type parameters
Name | Default | Description |
---|---|---|
UK | K |
Parameters
Name | Type | Description |
---|---|---|
key | RelatedTo <K, UK> | the key to look for |
value | V | the value to look for |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'])
m.hasEntry(1, 'a') // => true
m.hasEntry(1, 'b') // => false
Overrides
hasKey
Returns true if the given key
is present in the collection.
hasKey
key
is present in the collection.nonEmpty
Returns true since this collection is known to be non-empty
nonEmpty
Definition
nonEmpty(): this is
WithKeyValue
<Tp, K, V>['nonEmpty'];
HashMultiMapHashValue.of([1, 1], [2, 2]).nonEmpty() // => true
Overrides
removeEntries
Returns the collection where given entries
are removed.
removeEntries
entries
are removed.Definition
removeEntries<UK = K, UV = V>(entries:
StreamSource
<[
RelatedTo
<K, UK>,
RelatedTo
<V, UV>]>):
WithKeyValue
<Tp, K, V>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
UK | K | |
UV | V |
Parameters
Name | Type | Description |
---|---|---|
entries | StreamSource <[ RelatedTo <K, UK>, RelatedTo <V, UV>]> | a StreamSource containing key-value entries to remove |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c'])
m.removeEntries([[2, 'b'], [1, 'd']]).toArray() // => [[1, 'a'], [1, 'c']]
m.removeEntries([2, 'q']).toArray() // => [[1, 'a'], [2, 'b'], [1, 'c']]
m.removeEntries(3) === m // true
guarantees same object reference if the key is not present
Overrides
removeEntry
Returns the collection where given value
if removed from the values associated with given key
.
removeEntry
value
if removed from the values associated with given key
.Definition
removeEntry<UK = K, UV = V>(key:
RelatedTo
<K, UK>, value:
RelatedTo
<V, UV>):
WithKeyValue
<Tp, K, V>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
UK | K | |
UV | V |
Parameters
Name | Type | Description |
---|---|---|
key | RelatedTo <K, UK> | the key of the entry to remove |
value | RelatedTo <V, UV> | the value of the entry to remove |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c'])
m.removeEntry(2, 'b').toArray() // => [[1, 'a'], [1, 'c']]
m.removeEntry(2, 'q').toArray() // => [[1, 'a'], [2, 'b'], [1, 'c']]
m.removeEntry(3, 'a') === m // true
guarantees same object reference if the key is not present
Overrides
removeKey
Returns the collection where the values associated with given key
are removed.
removeKey
key
are removed.Definition
removeKey<UK = K>(key:
RelatedTo
<K, UK>):
WithKeyValue
<Tp, K, V>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
UK | K |
Parameters
Name | Type | Description |
---|---|---|
key | RelatedTo <K, UK> | the key of the entries to remove |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c'])
m.removeKey(2).toArray() // => [[1, 'a'], [1, 'c']]
m.removeKey(3) === m // true
guarantees same object reference if the key is not present
Overrides
removeKeyAndGet
Returns a tuple containing the collection of which the given key
is removed, and the values that are associated with that key. If the key is not present, it will return undefined instead.
removeKeyAndGet
key
is removed, and the values that are associated with that key. If the key is not present, it will return undefined instead.Definition
removeKeyAndGet<UK = K>(key:
RelatedTo
<K, UK>): [
WithKeyValue
<Tp, K, V>['normal'],
WithKeyValue
<Tp, K, V>['keyMapValuesNonEmpty']
]
|
undefined;
Type parameters
Name | Default | Description |
---|---|---|
UK | K |
Parameters
Name | Type | Description |
---|---|---|
key | RelatedTo <K, UK> | the key of the entry to remove |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'])
const result = m.removeKeyAndGet(2)
if (result !== undefined) console.log([result[0].toString(), result[1]]) // => logs [HashMultiMapHashValue(1 => 'a'), HashSet('b')]
console.log(m.removeKeyAndGet(3)) // => logs undefined
Overrides
removeKeys
Returns the collection where the values associated with given keys
are removed.
removeKeys
keys
are removed.Definition
removeKeys<UK = K>(keys:
StreamSource
<
RelatedTo
<K, UK>>):
WithKeyValue
<Tp, K, V>['normal'];
Type parameters
Name | Default | Description |
---|---|---|
UK | K |
Parameters
Name | Type | Description |
---|---|---|
keys | StreamSource < RelatedTo <K, UK>> | a StreamSource of keys to remove |
const m = HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c'])
m.removeKeys([2, 10]).toArray() // => [[1, 'a'], [1, 'c']]
m.removeKeys([10, 11]) === m // true
guarantees same object reference if the key is not present
Overrides
stream
Returns a non-empty Stream
of the elements in this collection.
stream
Stream
of the elements in this collection.Definition
stream():
Stream.NonEmpty
<T>;
Overrides
NonEmpty.stream, NonEmpty.stream, VariantMultiMapBase.stream
streamKeys
Returns a non-empty Stream containing all keys of this collection.
streamKeys
Definition
streamKeys():
Stream.NonEmpty
<K>;
HashMultiMapHashValue.of([[1, 'a'], [2, 'b']]).streamKeys().toArray() // => [1, 2]
Overrides
streamValues
Returns a non-empty Stream containing all values of this collection.
streamValues
Definition
streamValues():
Stream.NonEmpty
<V>;
HashMultiMapHashValue.of([[1, 'a'], [2, 'b']]).streamValues().toArray() // => ['a', 'b']
Overrides
toArray
Returns a non-empty array containing all entries in this collection.
toArray
Definition
toArray():
ArrayNonEmpty
<[K, V]>;
HashMultiMapHashValue.of([1, 'a'], [2, 'b'], [1, 'c']).toArray() // => [[1, 'a'], [1, 'c'], [2, 'b']]
O(log(N)) @note it is safe to mutate the returned array, however, the array elements are not copied, thus should be treated as read-only
Overrides
toJSON
Returns a JSON representation of this collection.
toJSON