Class NonEmptyMap<K,V>
- Type Parameters:
K- the type of keysV- the type of values
This type makes the non-emptiness constraint part of the static type system.
APIs that require at least one entry can declare NonEmptyMap<K, V> instead of
Map<K, V> and eliminate runtime emptiness checks entirely.
Insertion order is preserved (backed by LinkedHashMap). The first entry
inserted is the headKey() / headValue().
This class is @NullMarked: all keys, values, and parameters are non-null
by default.
The rationale for introducing dedicated non-empty collection types instead of using standard JDK types with runtime checks is documented in ADR-018 — NonEmptyList<T>, NonEmptySet<T>, NonEmptyMap<K,V> as structural guarantee types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,K, V> Collector <T, ?, Option<NonEmptyMap<K, V>>> Returns aCollectorthat accumulates aStream<T>into anOption<NonEmptyMap<K, V>>using the provided key and value extractor functions.booleancontainsKey(K key) Returnstrueif this map contains an entry forkey.booleanOption<NonEmptyMap<K, V>> filter(BiPredicate<? super K, ? super V> predicate) Returns a newNonEmptyMapcontaining only entries for whichpredicatereturnstrue, wrapped inOption.some(Object).static <K,V> Option <NonEmptyMap<K, V>> Attempts to construct aNonEmptyMapfrom a plainMap.static <K,V> Option <NonEmptyMap<K, V>> fromOptional(Optional<? extends Map<? extends K, ? extends V>> optional) Returns the value associated withkey, orOption.none()if absent.inthashCode()headKey()Returns the guaranteed head key of this map.Returns the value associated with the head key.keySet()Returns aNonEmptySetcontaining all keys of this map in insertion order.<R> NonEmptyMap<R, V> Appliesmapperto every key and returns a newNonEmptyMapwith the mapped keys and the original values.<R> NonEmptyMap<K, R> Appliesmapperto every value and returns a newNonEmptyMapwith the same keys and mapped values.<R> NonEmptyMap<K, R> mapValuesWithKey(BiFunction<? super K, ? super V, ? extends R> mapper) Appliesmapperto every key-value pair and returns a newNonEmptyMapwith the same keys and mapped values.merge(NonEmptyMap<K, V> other, BinaryOperator<V> mergeFunction) Returns a newNonEmptyMapthat is the union of this map andother.static <K,V> NonEmptyMap <K, V> Creates aNonEmptyMapwith the given head entry and additional entries.static <K,E, V> Either <E, NonEmptyMap<K, V>> sequenceEither(NonEmptyMap<K, Either<E, V>> nem) Sequences aNonEmptyMap<K, Either<E, V>>into anEither<E, NonEmptyMap<K, V>>.static <K,V> Option <NonEmptyMap<K, V>> sequenceOption(NonEmptyMap<K, Option<V>> nem) Sequences aNonEmptyMap<K, Option<V>>into anOption<NonEmptyMap<K, V>>.static <K,V, E> Result <NonEmptyMap<K, V>, E> sequenceResult(NonEmptyMap<K, Result<V, E>> nem) Sequences aNonEmptyMap<K, Result<V, E>>into aResult<NonEmptyMap<K, V>, E>.static <K,V> Try <NonEmptyMap<K, V>> sequenceTry(NonEmptyMap<K, Try<V>> nem) Sequences aNonEmptyMap<K, Try<V>>into aTry<NonEmptyMap<K, V>>.static <K,V> NonEmptyMap <K, V> singleton(K key, V value) Creates aNonEmptyMapcontaining exactly one entry.intsize()Returns the number of entries in this map.toMap()Returns an unmodifiableMapcontaining all entries (head entry first, then tail entries in insertion order).Converts this map to aNonEmptyListof its entries in insertion order.toStream()Returns a sequentialStreamof all entries in insertion order.toString()values()Returns aNonEmptyListcontaining all values of this map in insertion order.
-
Method Details
-
of
Creates aNonEmptyMapwith the given head entry and additional entries.If
restcontainskey, that duplicate is ignored — the providedvalueis always used for the head key.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the head key; must not benullvalue- the head value; must not benullrest- additional entries; must not benull; keys and values must not benull- Returns:
- a new
NonEmptyMap - Throws:
NullPointerException- ifkey,value,rest, or any key/value insiderestisnull
-
singleton
Creates aNonEmptyMapcontaining exactly one entry.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the sole key; must not benullvalue- the sole value; must not benull- Returns:
- a singleton
NonEmptyMap - Throws:
NullPointerException- ifkeyorvalueisnull
-
fromMap
Attempts to construct aNonEmptyMapfrom a plainMap.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map; must not benull; keys and values must not benull- Returns:
Option.some(Object)wrapping theNonEmptyMapif the map is non-empty, orOption.none()if the map is empty- Throws:
NullPointerException- ifmapor any key/value isnull
-
fromOptional
public static <K,V> Option<NonEmptyMap<K,V>> fromOptional(Optional<? extends Map<? extends K, ? extends V>> optional) Attempts to construct aNonEmptyMapfrom a JDKOptionalwrapping a plainMap.If the optional is present and the wrapped map is non-empty, returns
Option.some(Object)wrapping theNonEmptyMap. If the optional is empty, or if the wrapped map is itself empty, returnsOption.none().- Type Parameters:
K- the key typeV- the value type- Parameters:
optional- the source optional; must not benull- Returns:
Option.some(Object)if the optional is present and the map is non-empty,Option.none()otherwise- Throws:
NullPointerException- ifoptionalisnull
-
collector
public static <T,K, Collector<T, ?, Option<NonEmptyMap<K,V> V>>> collector(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Returns aCollectorthat accumulates aStream<T>into anOption<NonEmptyMap<K, V>>using the provided key and value extractor functions.Produces
Option.some(Object)for a non-empty stream andOption.none()for an empty stream. If multiple stream elements map to the same key, later elements overwrite earlier ones (last-write-wins semantics).Example:
Option<NonEmptyMap<String, Integer>> result = employees.stream() .collect(NonEmptyMap.collector(Employee::name, Employee::score));- Type Parameters:
T- the stream element typeK- the key typeV- the value type- Parameters:
keyMapper- extracts the key from each element; must not benullor returnnullvalueMapper- extracts the value from each element; must not benullor returnnull- Returns:
- a collector producing
Option<NonEmptyMap<K, V>> - Throws:
NullPointerException- ifkeyMapperorvalueMapperisnull
-
headKey
-
headValue
Returns the value associated with the head key.- Returns:
- the head value (never
null)
-
size
public int size()Returns the number of entries in this map. Always ≥ 1.- Returns:
- the size
-
get
Returns the value associated withkey, orOption.none()if absent.- Parameters:
key- the key to look up; must not benull- Returns:
Some(value)if the key is present,Noneotherwise- Throws:
NullPointerException- ifkeyisnull
-
containsKey
Returnstrueif this map contains an entry forkey.- Parameters:
key- the key to test; must not benull- Returns:
trueif the key is present- Throws:
NullPointerException- ifkeyisnull
-
keySet
Returns aNonEmptySetcontaining all keys of this map in insertion order. The head key of this map is the head of the returned set.- Returns:
- a
NonEmptySet<K>of all keys (nevernull)
-
values
Returns aNonEmptyListcontaining all values of this map in insertion order. The head value of this map is the head of the returned list. Duplicate values are preserved (unlike keys, values need not be unique).- Returns:
- a
NonEmptyList<V>of all values (nevernull)
-
toMap
-
toStream
-
mapValues
Appliesmapperto every value and returns a newNonEmptyMapwith the same keys and mapped values.- Type Parameters:
R- the result value type- Parameters:
mapper- a non-null function to apply to each value; must not returnnull- Returns:
- a new
NonEmptyMapwith mapped values - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
mapValuesWithKey
Appliesmapperto every key-value pair and returns a newNonEmptyMapwith the same keys and mapped values.Unlike
mapValues(Function), the mapper receives both the key and the value, enabling value transformations that depend on the key.- Type Parameters:
R- the result value type- Parameters:
mapper- a non-null function to apply to each key-value pair; must not returnnull- Returns:
- a new
NonEmptyMapwith mapped values - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
mapKeys
Appliesmapperto every key and returns a newNonEmptyMapwith the mapped keys and the original values.If multiple keys map to the same new key, head-wins semantics apply: the head entry is always preserved, and any tail entry whose mapped key collides with the mapped head key is silently dropped.
- Type Parameters:
R- the result key type- Parameters:
mapper- a non-null function to apply to each key; must not returnnull- Returns:
- a new
NonEmptyMapwith mapped keys - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
filter
Returns a newNonEmptyMapcontaining only entries for whichpredicatereturnstrue, wrapped inOption.some(Object). ReturnsOption.none()if no entries pass the predicate.- Parameters:
predicate- a non-null predicate to test each key-value pair- Returns:
Some(filteredMap)if at least one entry passes,Noneotherwise- Throws:
NullPointerException- ifpredicateisnull
-
merge
Returns a newNonEmptyMapthat is the union of this map andother. When both maps contain the same key,mergeFunctionis applied to the two values.- Parameters:
other- the other map; must not benullmergeFunction- function to resolve value conflicts; must not benull; must not returnnull(a null return would violate the non-null value contract and is rejected immediately)- Returns:
- a new
NonEmptyMapcontaining all entries from both maps - Throws:
NullPointerException- ifother,mergeFunction, or the result ofmergeFunctionisnull
-
toNonEmptyList
Converts this map to aNonEmptyListof its entries in insertion order.- Returns:
- a
NonEmptyList<Map.Entry<K, V>>
-
sequenceOption
Sequences aNonEmptyMap<K, Option<V>>into anOption<NonEmptyMap<K, V>>.Returns
Option.some(Object)containing all unwrapped values if every entry's value isSome; returnsOption.none()as soon as any entry's value isNone(fail-fast in inspection — the method stops iterating after the firstNone; entries are already materialized in the map before sequencing, so later entries are not inspected but were already evaluated).- Type Parameters:
K- the key typeV- the unwrapped value type- Parameters:
nem- aNonEmptyMap<K, Option<V>>; must not benull- Returns:
Some(NonEmptyMap<K, V>)if all values are present,Noneotherwise- Throws:
NullPointerException- ifnemisnull
-
sequenceTry
Sequences aNonEmptyMap<K, Try<V>>into aTry<NonEmptyMap<K, V>>.Returns
Try.success(Object)if all values succeed; returnsTry.failure(Throwable)from the first failing entry (fail-fast in inspection — the method stops iterating after the first failure; entries are already materialized in the map before sequencing, so later entries are not inspected but were already evaluated).- Type Parameters:
K- the key typeV- the success value type- Parameters:
nem- aNonEmptyMap<K, Try<V>>; must not benull- Returns:
Success(NonEmptyMap<K, V>)if all succeed,Failureotherwise- Throws:
NullPointerException- ifnemisnull
-
sequenceEither
Sequences aNonEmptyMap<K, Either<E, V>>into anEither<E, NonEmptyMap<K, V>>.Returns
Either.right(Object)if all values are right; returnsEither.left(Object)from the first left entry (fail-fast in inspection — the method stops iterating after the first left; entries are already materialized in the map before sequencing, so later entries are not inspected but were already evaluated).- Type Parameters:
K- the key typeE- the left (error) typeV- the right (success) type- Parameters:
nem- aNonEmptyMap<K, Either<E, V>>; must not benull- Returns:
right(NonEmptyMap<K, V>)if all are right,left(E)otherwise- Throws:
NullPointerException- ifnemisnull
-
sequenceResult
Sequences aNonEmptyMap<K, Result<V, E>>into aResult<NonEmptyMap<K, V>, E>.Returns
Result.ok(Object)if all values are ok; returnsResult.err(Object)from the first error entry (fail-fast in inspection — the method stops iterating after the first err; entries are already materialized in the map before sequencing, so later entries are not inspected but were already evaluated).- Type Parameters:
K- the key typeV- the ok value typeE- the error type- Parameters:
nem- aNonEmptyMap<K, Result<V, E>>; must not benull- Returns:
ok(NonEmptyMap<K, V>)if all succeed,err(E)otherwise- Throws:
NullPointerException- ifnemisnull
-
equals
-
hashCode
-
toString
-