Class NonEmptyList<T>
- Type Parameters:
T- the type of elements in this list
- All Implemented Interfaces:
Iterable<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.
-
Method Summary
Modifier and TypeMethodDescriptionReturns a newNonEmptyListwithelementappended at the end.static <T> Collector<T, ?, Option<NonEmptyList<T>>> concat(NonEmptyList<T> other) Returns a newNonEmptyListthat is the concatenation of this list andother.booleanstatic <T> Option<NonEmptyList<T>> Attempts to construct aNonEmptyListfrom a plainList.inthashCode()head()Returns the first element of this list.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.static <T> Option<NonEmptyList<T>> sequence(NonEmptyList<Option<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.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 Iterable
forEach, spliterator
-
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
-
head
-
tail
-
toList
-
size
public int size()Returns the number of elements in this list. Always ≥ 1.- 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
-
iterator
-
equals
-
hashCode
-
toString
-