Class NonEmptyList<T>
- Type Parameters:
T- the type of elements in this list
- All Implemented Interfaces:
Iterable<T>, Collection<T>, SequencedCollection<T>
This type makes the non-emptiness constraint part of the static type system.
APIs that require at least one item can declare NonEmptyList<T> instead of
List<T> and eliminate runtime emptiness checks entirely.
NonEmptyList<T> pairs naturally with Validated's error accumulation,
which always produces at least one error when invalid.
This class is @NullMarked: all elements 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 TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> c) voidUnsupported —NonEmptyListis immutable.voidUnsupported —NonEmptyListis immutable.Returns a newNonEmptyListwithelementappended at the end.voidclear()static <T> Collector<T, ?, Option<NonEmptyList<T>>> concat(NonEmptyList<T> other) Returns a newNonEmptyListthat is the concatenation of this list andother.booleanbooleancontainsAll(Collection<?> c) booleanstatic <T> Option<NonEmptyList<T>> Attempts to construct aNonEmptyListfrom a plainList.static <T> Option<NonEmptyList<T>> fromOptional(Optional<? extends T> optional) Attempts to construct a singletonNonEmptyListfrom a JDKOptional.getFirst()Returns the first element of this list.getLast()Returns the last element of this list.inthashCode()head()Returns the first element of this list.booleanisEmpty()Alwaysfalse— aNonEmptyListalways has at least one element.iterator()Returns an iterator over all elements (head first, then tail).<R> NonEmptyList<R> Appliesmapperto every element and returns a newNonEmptyListof the results.static <T> NonEmptyList<T> Creates aNonEmptyListwith the given head and tail.Returns a newNonEmptyListwithelementprepended at the front.booleanbooleanremoveAll(Collection<?> c) Unsupported —NonEmptyListis immutable.booleanUnsupported —NonEmptyListis immutable.booleanretainAll(Collection<?> c) reversed()Returns a newNonEmptyListwith all elements in reverse order.static <T> Option<NonEmptyList<T>> sequence(NonEmptyList<Option<T>> nel) static <E,T> Either <E, NonEmptyList<T>> sequenceEither(NonEmptyList<Either<E, T>> nel) static <T,E> Result <NonEmptyList<T>, E> sequenceResult(NonEmptyList<Result<T, E>> nel) static <T> Try<NonEmptyList<T>> sequenceTry(NonEmptyList<Try<T>> nel) static <T> NonEmptyList<T> singleton(T head) Creates aNonEmptyListcontaining exactly one element.intsize()Returns the number of elements in this list.tail()Returns an unmodifiable view of all elements after the head.Object[]toArray()<E> E[]toArray(E[] a) toList()Returns an unmodifiableListcontaining all elements (head followed by tail).static <T> Collector<T, ?, Option<NonEmptyList<T>>> Alias forcollector().Converts this list to aNonEmptySet, preserving the head element and deduplicating the tail in insertion order.toStream()Returns a sequentialStreamof all elements (head first, then tail).toString()Methods inherited from interface Collection
parallelStream, spliterator, stream, toArray
-
Method Details
-
of
Creates aNonEmptyListwith the given head and tail.- Type Parameters:
T- the element type- Parameters:
head- the first (mandatory) element; must not benulltail- the remaining elements; must not benull; elements must not benull- Returns:
- a new
NonEmptyList - Throws:
NullPointerException- ifhead,tail, or any tail element isnull
-
singleton
Creates aNonEmptyListcontaining exactly one element.- Type Parameters:
T- the element type- Parameters:
head- the sole element; must not benull- Returns:
- a singleton
NonEmptyList - Throws:
NullPointerException- ifheadisnull
-
fromList
Attempts to construct aNonEmptyListfrom a plainList.- Type Parameters:
T- the element type- Parameters:
list- the source list; must not benull; elements must not benull- Returns:
Option.some(Object)wrapping theNonEmptyListif the list is non-empty, orOption.none()if the list is empty- Throws:
NullPointerException- iflistor any element isnull
-
fromOptional
Attempts to construct a singletonNonEmptyListfrom a JDKOptional.- Type Parameters:
T- the element type- Parameters:
optional- the source optional; must not benull- Returns:
Option.some(Object)wrapping a singletonNonEmptyListif the optional is present, orOption.none()if the optional is empty- Throws:
NullPointerException- ifoptionalisnull
-
head
-
getFirst
Returns the first element of this list. Alias forhead().Implements
SequencedCollection.getFirst().- Specified by:
getFirstin interfaceSequencedCollection<T>- Returns:
- the first element (never
null)
-
getLast
Returns the last element of this list.Implements
SequencedCollection.getLast().- Specified by:
getLastin interfaceSequencedCollection<T>- Returns:
- the last element (never
null)
-
reversed
Returns a newNonEmptyListwith all elements in reverse order.Implements
SequencedCollection.reversed().- Specified by:
reversedin interfaceSequencedCollection<T>- Returns:
- a reversed copy of this list
-
addFirst
Unsupported —NonEmptyListis immutable.- Specified by:
addFirstin interfaceSequencedCollection<T>- Throws:
UnsupportedOperationException- always
-
addLast
Unsupported —NonEmptyListis immutable.- Specified by:
addLastin interfaceSequencedCollection<T>- Throws:
UnsupportedOperationException- always
-
removeFirst
Unsupported —NonEmptyListis immutable.- Specified by:
removeFirstin interfaceSequencedCollection<T>- Throws:
UnsupportedOperationException- always
-
removeLast
Unsupported —NonEmptyListis immutable.- Specified by:
removeLastin interfaceSequencedCollection<T>- Throws:
UnsupportedOperationException- always
-
tail
-
toList
-
size
public int size()Returns the number of elements in this list. Always ≥ 1.- Specified by:
sizein interfaceCollection<T>- Returns:
- the size
-
map
Appliesmapperto every element and returns a newNonEmptyListof the results. The structure (head/tail split) is preserved.- Type Parameters:
R- the result element type- Parameters:
mapper- a non-null function to apply to each element; must not returnnull- Returns:
- a new
NonEmptyListof mapped values - Throws:
NullPointerException- ifmapperisnullor returnsnull
-
append
Returns a newNonEmptyListwithelementappended at the end.- Parameters:
element- the element to append; must not benull- Returns:
- a new
NonEmptyListwith the element appended - Throws:
NullPointerException- ifelementisnull
-
prepend
Returns a newNonEmptyListwithelementprepended at the front.- Parameters:
element- the element to prepend; must not benull- Returns:
- a new
NonEmptyListwith the element prepended - Throws:
NullPointerException- ifelementisnull
-
concat
Returns a newNonEmptyListthat is the concatenation of this list andother.- Parameters:
other- the list to append; must not benull- Returns:
- a new
NonEmptyListcontaining all elements of both lists - Throws:
NullPointerException- ifotherisnull
-
toNonEmptySet
Converts this list to aNonEmptySet, preserving the head element and deduplicating the tail in insertion order. Duplicate elements are silently dropped; the head is always retained.- Returns:
- a
NonEmptySet<T>with the same distinct elements
-
toStream
-
collector
Returns aCollectorthat accumulates aStream<T>into anOption<NonEmptyList<T>>.Produces
Option.some(Object)if the stream has at least one element, orOption.none()if the stream is empty.- Type Parameters:
T- the element type- Returns:
- a
CollectorproducingOption<NonEmptyList<T>>
-
toNonEmptyList
Alias forcollector().Returns a
Collectorthat accumulates aStream<T>into anOption<NonEmptyList<T>>. ProducesOption.some(Object)if the stream has at least one element, orOption.none()if the stream is empty.Option<NonEmptyList<String>> tags = Stream.of("java", "fp", "dmx-fun") .collect(NonEmptyList.toNonEmptyList()); // Some(["java", "fp", "dmx-fun"]) Option<NonEmptyList<String>> empty = Stream.<String>empty() .collect(NonEmptyList.toNonEmptyList()); // None- Type Parameters:
T- the element type- Returns:
- a
CollectorproducingOption<NonEmptyList<T>>
-
sequence
Sequences aNonEmptyListofOptions into anOptionof aNonEmptyList.Returns
Option.some(Object)containing all unwrapped values if every element isOption.some(Object); returnsOption.none()as soon as any element isOption.none().- Type Parameters:
T- the element type- Parameters:
nel- aNonEmptyList<Option<T>>; must not benull- Returns:
Some(NonEmptyList<T>)if all options are present,Noneotherwise- Throws:
NullPointerException- ifnelisnull
-
sequenceTry
Sequences aNonEmptyListofTrys into aTryof aNonEmptyList.Returns
Try.success(Object)containing all unwrapped values if every element is a success; returnsTry.failure(Throwable)from the first failing element (fail-fast in inspection — the method stops iterating after the first failure; elements are already materialized in the list before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
T- the success value type- Parameters:
nel- aNonEmptyList<Try<T>>; must not benull- Returns:
Success(NonEmptyList<T>)if all succeed,Failurefrom the first failing element otherwise- Throws:
NullPointerException- ifnelisnull
-
sequenceEither
Sequences aNonEmptyListofEithers into anEitherof aNonEmptyList.Returns
Either.right(Object)containing all unwrapped values if every element is right; returnsEither.left(Object)from the first left element (fail-fast in inspection — the method stops iterating after the first left; elements are already materialized in the list before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
E- the left (error) typeT- the right (success) type- Parameters:
nel- aNonEmptyList<Either<E, T>>; must not benull- Returns:
right(NonEmptyList<T>)if all are right,left(E)from the first left element otherwise- Throws:
NullPointerException- ifnelisnull
-
sequenceResult
Sequences aNonEmptyListofResults into aResultof aNonEmptyList.Returns
Result.ok(Object)containing all unwrapped values if every element is ok; returnsResult.err(Object)from the first error element (fail-fast in inspection — the method stops iterating after the first err; elements are already materialized in the list before sequencing, so later elements are not inspected but were already evaluated).- Type Parameters:
T- the ok value typeE- the error type- Parameters:
nel- aNonEmptyList<Result<T, E>>; must not benull- Returns:
ok(NonEmptyList<T>)if all succeed,err(E)from the first error element otherwise- Throws:
NullPointerException- ifnelisnull
-
isEmpty
public boolean isEmpty()Alwaysfalse— aNonEmptyListalways has at least one element.- Specified by:
isEmptyin interfaceCollection<T>
-
contains
- Specified by:
containsin interfaceCollection<T>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>
-
toArray
- Specified by:
toArrayin interfaceCollection<T>
-
toArray
public <E> E[] toArray(E[] a) - Specified by:
toArrayin interfaceCollection<T>
-
add
- Specified by:
addin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
remove
- Specified by:
removein interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
addAll
- Specified by:
addAllin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
retainAll
- Specified by:
retainAllin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
removeIf
- Specified by:
removeIfin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>- Throws:
UnsupportedOperationException- always —NonEmptyListis immutable
-
iterator
-
equals
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T>- Overrides:
hashCodein classObject
-
toString
-