Interface Value<T>
- Type Parameters:
T
- The type of the wrapped value.
- All Superinterfaces:
Iterable<T>
- All Known Subinterfaces:
BitSet<T>
,Either<L,
,R> Future<T>
,IndexedSeq<T>
,Iterator<T>
,LinearSeq<T>
,List<T>
,Map<K,
,V> Multimap<K,
,V> Option<T>
,Seq<T>
,Set<T>
,SortedMap<K,
,V> SortedMultimap<K,
,V> SortedSet<T>
,Stream<T>
,Traversable<T>
,Tree<T>
,Try<T>
,Validation<E,
T>
- All Known Implementing Classes:
Array
,CharSeq
,Either.Left
,Either.LeftProjection
,Either.Right
,Either.RightProjection
,HashMap
,HashMultimap
,HashSet
,Lazy
,LinkedHashMap
,LinkedHashMultimap
,LinkedHashSet
,List.Cons
,List.Nil
,Option.None
,Option.Some
,PriorityQueue
,Queue
,Stream.Cons
,Stream.Empty
,Tree.Empty
,Tree.Node
,TreeMap
,TreeMultimap
,TreeSet
,Try.Failure
,Try.Success
,Validation.Invalid
,Validation.Valid
,Vector
Value
type reflects the values in a functional setting. It can be seen as the result of a partial function application.
Hence the result may be undefined. If a value is undefined, we say it is empty.
How the empty state is interpreted depends on the context, i.e. it may be undefined, failed, no elements, etc.
Basic operations:
get()
getOrElse(Object)
getOrElse(Supplier)
getOrElseThrow(Supplier)
getOrElseTry(CheckedFunction0)
getOrNull()
map(Function)
stringPrefix()
collect(Collector)
collect(Supplier, BiConsumer, BiConsumer)
toArray()
toCharSeq()
toEither(Object)
toEither(Supplier)
toInvalid(Object)
toInvalid(Supplier)
toJavaArray()
toJavaArray(Class)
toJavaCollection(Function)
toJavaList()
toJavaList(Function)
toJavaMap(Function)
toJavaMap(Supplier, Function)
toJavaMap(Supplier, Function, Function)
toJavaOptional()
toJavaParallelStream()
toJavaSet()
toJavaSet(Function)
toJavaStream()
toLeft(Object)
toLeft(Supplier)
toLinkedMap(Function)
toLinkedMap(Function, Function)
toLinkedSet()
toList()
toMap(Function)
toMap(Function, Function)
toOption()
toPriorityQueue()
toPriorityQueue(Comparator)
toQueue()
toRight(Object)
toRight(Supplier)
toSet()
toSortedMap(Comparator, Function)
toSortedMap(Comparator, Function, Function)
toSortedMap(Function)
toSortedMap(Function, Function)
toSortedSet()
toSortedSet(Comparator)
toStream()
toString()
toTree()
toTry()
toTry(Supplier)
toValid(Object)
toValid(Supplier)
toValidation(Object)
toValidation(Supplier)
toVector()
- Author:
- Daniel Dietrich
-
Method Summary
Modifier and TypeMethodDescriptiondefault <R> R
collect
(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) Collects the underlying value(s) (if present) using the givensupplier
,accumulator
andcombiner
.default <R,
A> R Collects the underlying value(s) (if present) using the providedcollector
.default boolean
Shortcut forexists(e -> Objects.equals(e, element))
, tests if the givenelement
is contained.default <U> boolean
corresponds
(Iterable<U> that, BiPredicate<? super T, ? super U> predicate) Tests whether every element of this iterable relates to the corresponding element of another iterable by satisfying a test predicate.default boolean
A smoothing replacement forequals
.boolean
Clarifies that values have a proper equals() method implemented.default boolean
Checks, if an element exists such that the predicate holds.default boolean
Checks, if the given predicate holds for all elements.default void
Performs an action on each element.get()
Gets the underlying value or throws if no value is present.default T
Returns the underlying value if present, otherwiseother
.default T
Returns the underlying value if present, otherwiseother
.getOrElseThrow
(Supplier<X> supplier) Returns the underlying value if present, otherwise throwssupplier.get()
.default T
getOrElseTry
(CheckedFunction0<? extends T> supplier) Returns the underlying value if present, otherwise returns the result ofTry.of(supplier).get()
.default T
Returns the underlying value if present, otherwisenull
.int
hashCode()
Clarifies that values have a proper hashCode() method implemented.boolean
isAsync()
Checks if thisValue
is asynchronously (short: async) computed.boolean
isEmpty()
Checks, thisValue
is empty, i.e. if the underlying value is absent.boolean
isLazy()
Checks if thisValue
is lazily evaluated.boolean
States whether this is a single-valued type.iterator()
Returns a richio.vavr.collection.Iterator
.<U> Value
<U> Maps the underlying value to a different component type.static <T> Value
<T> Narrows a widenedValue<? extends T>
toValue<T>
by performing a type-safe cast.default void
out
(PrintStream out) Sends the string representations of this to thePrintStream
.default void
out
(PrintWriter writer) Sends the string representations of this to thePrintWriter
.Performs the givenaction
on the first element if this is an eager implementation.default Spliterator
<T> default void
stderr()
Sends the string representations of this to the standard error stream System.err.default void
stdout()
Sends the string representations of this to the standard output stream System.out.Returns the name of this Value type, which is used by toString().toArray()
Converts this to aArray
.default CharSeq
Converts this to aCharSeq
.default CompletableFuture
<T> Converts this to aCompletableFuture
Converts this to anEither
.toEither
(L left) Converts this to anEither
.default <U> Validation
<T, U> Deprecated.default <U> Validation
<T, U> toInvalid
(U value) Deprecated.UsetoValidation(Object)
instead.default Object[]
Converts this to a Java array with component typeObject
default T[]
toJavaArray
(Class<T> componentType) Deprecated.UsetoJavaArray(IntFunction)
insteaddefault T[]
toJavaArray
(IntFunction<T[]> arrayFactory) Converts this to a Java array having an accurate component type.default <C extends Collection<T>>
CtoJavaCollection
(Function<Integer, C> factory) Converts this to a specific mutableCollection
of typeC
.Converts this to a mutableList
.toJavaList
(Function<Integer, LIST> factory) Converts this to a specific mutableList
.default <K,
V> Map <K, V> Converts this to a mutableMap
.default <K,
V, MAP extends Map<K, V>>
MAPConverts this to a specific mutableMap
.default <K,
V, MAP extends Map<K, V>>
MAPtoJavaMap
(Supplier<MAP> factory, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to a specific mutableMap
.Converts this to anOptional
.Converts this to a parallelStream
by callingStreamSupport.stream(this.spliterator(), true)
.Converts this to a mutableSet
.Converts this to a specificSet
.Converts this to a sequentialStream
by callingStreamSupport.stream(this.spliterator(), false)
.Deprecated.UsetoEither(Supplier)
instead.toLeft
(R right) Deprecated.UsetoEither(Object)
instead.default <K,
V> Map <K, V> toLinkedMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.default <K,
V> Map <K, V> toLinkedMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.Converts this to aSet
.toList()
Converts this to aList
.default <K,
V> Map <K, V> Converts this to aMap
.default <K,
V> Map <K, V> Converts this to aMap
.toOption()
Converts this to anOption
.default PriorityQueue
<T> Converts this to aPriorityQueue
.default PriorityQueue
<T> toPriorityQueue
(Comparator<? super T> comparator) Converts this to aPriorityQueue
.toQueue()
Converts this to aQueue
.Deprecated.UsetoEither(Supplier)
instead.toRight
(L left) Deprecated.UsetoEither(Object)
instead.toSet()
Converts this to aSet
.default <K,
V> SortedMap <K, V> toSortedMap
(Comparator<? super K> comparator, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.default <K,
V> SortedMap <K, V> toSortedMap
(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.default <K extends Comparable<? super K>,
V>
SortedMap<K, V> toSortedMap
(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.default <K extends Comparable<? super K>,
V>
SortedMap<K, V> toSortedMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.Converts this to aSortedSet
.toSortedSet
(Comparator<? super T> comparator) Converts this to aSortedSet
.toStream()
Converts this to aStream
.toString()
Clarifies that values have a proper toString() method implemented.toTree()
Converts this to aTree
.toTry()
Converts this to aTry
.Converts this to aTry
.default <E> Validation
<E, T> toValid
(E error) Deprecated.UsetoValidation(Object)
instead.default <E> Validation
<E, T> Deprecated.UsetoValidation(Supplier)
instead.default <E> Validation
<E, T> toValidation
(E invalid) Converts this to anValidation
.default <E> Validation
<E, T> toValidation
(Supplier<? extends E> invalidSupplier) Converts this to anValidation
.toVector()
Converts this to aVector
.
-
Method Details
-
narrow
Narrows a widenedValue<? extends T>
toValue<T>
by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T
- Component type of theValue
.- Parameters:
value
- AValue
.- Returns:
- the given
value
instance as narrowed typeValue<T>
.
-
collect
Collects the underlying value(s) (if present) using the providedcollector
.- Type Parameters:
R
- the result type of the reduction operationA
- the mutable accumulation type of the reduction operation- Parameters:
collector
- Collector performing reduction- Returns:
- R reduction result
-
collect
default <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) Collects the underlying value(s) (if present) using the givensupplier
,accumulator
andcombiner
.- Type Parameters:
R
- type of the result- Parameters:
supplier
- provide unit value for reductionaccumulator
- perform reduction with unit valuecombiner
- function for combining two values, which must be compatible with the accumulator.- Returns:
- R reduction result
-
contains
Shortcut forexists(e -> Objects.equals(e, element))
, tests if the givenelement
is contained.- Parameters:
element
- An Object of type A, may be null.- Returns:
- true, if element is contained, false otherwise.
-
corresponds
Tests whether every element of this iterable relates to the corresponding element of another iterable by satisfying a test predicate.- Type Parameters:
U
- Component type of that iterable- Parameters:
that
- the other iterablepredicate
- the test predicate, which relates elements from both iterables- Returns:
true
if both iterables have the same length andpredicate(x, y)
istrue
for all corresponding elementsx
of this iterable andy
ofthat
, otherwisefalse
.
-
eq
A smoothing replacement forequals
. It is similar to Scala's==
but better in the way that it is not limited to collection types, e.g.Some(1) eq List(1)
,None eq Failure(x)
etc.In a nutshell: eq checks congruence of structures and equality of contained values.
Example:
// ((1, 2), ((3))) => structure: (()(())) values: 1, 2, 3 final Value<?> i1 = List.of(List.of(1, 2), Arrays.asList(List.of(3))); final Value<?> i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3))); assertThat(i1.eq(i2)).isTrue();
Semantics:
o == this : true o instanceof Value : iterable elements are eq, non-iterable elements equals, for all (o1, o2) in (this, o) o instanceof Iterable : this eq Iterator.of((Iterable<?>) o); otherwise : false
- Parameters:
o
- An object- Returns:
- true, if this equals o according to the rules defined above, otherwise false.
-
exists
Checks, if an element exists such that the predicate holds.- Parameters:
predicate
- A Predicate- Returns:
- true, if predicate holds for one or more elements, false otherwise
- Throws:
NullPointerException
- ifpredicate
is null
-
forAll
Checks, if the given predicate holds for all elements.- Parameters:
predicate
- A Predicate- Returns:
- true, if the predicate holds for all elements, false otherwise
- Throws:
NullPointerException
- ifpredicate
is null
-
forEach
Performs an action on each element.- Specified by:
forEach
in interfaceIterable<T>
- Parameters:
action
- AConsumer
- Throws:
NullPointerException
- ifaction
is null
-
get
T get()Gets the underlying value or throws if no value is present.IMPORTANT! This method will throw an undeclared
Throwable
ifisEmpty() == true
is true.Because the 'empty' state indicates that there is no value present that can be returned,
get()
has to throw in such a case. Generally, implementing classes should throw aNoSuchElementException
ifisEmpty()
returns true.However, there exist use-cases, where implementations may throw other exceptions. See
Try.get()
.Additional note: Dynamic proxies will wrap an undeclared exception in a
UndeclaredThrowableException
.- Returns:
- the underlying value if this is not empty, otherwise
get()
throws aThrowable
-
getOrElse
Returns the underlying value if present, otherwiseother
.- Parameters:
other
- An alternative value.- Returns:
- A value of type
T
-
getOrElse
Returns the underlying value if present, otherwiseother
.- Parameters:
supplier
- An alternative value supplier.- Returns:
- A value of type
T
- Throws:
NullPointerException
- if supplier is null
-
getOrElseThrow
Returns the underlying value if present, otherwise throwssupplier.get()
.- Type Parameters:
X
- a Throwable type- Parameters:
supplier
- An exception supplier.- Returns:
- A value of type
T
. - Throws:
NullPointerException
- if supplier is nullX
- if no value is present
-
getOrElseTry
Returns the underlying value if present, otherwise returns the result ofTry.of(supplier).get()
.- Parameters:
supplier
- An alternative value supplier.- Returns:
- A value of type
T
. - Throws:
NullPointerException
- if supplier is null
-
getOrNull
Returns the underlying value if present, otherwisenull
.- Returns:
- A value of type
T
ornull
.
-
isAsync
boolean isAsync()Checks if thisValue
is asynchronously (short: async) computed.Methods of a
Value
instance that operate on the underlying value may block the current thread until the value is present and the computation can be performed.- Returns:
- true if this
Value
is async (likeFuture
), false otherwise.
-
isEmpty
boolean isEmpty()Checks, thisValue
is empty, i.e. if the underlying value is absent.- Returns:
- false, if no underlying value is present, true otherwise.
-
isLazy
boolean isLazy()Checks if thisValue
is lazily evaluated. -
isSingleValued
boolean isSingleValued()States whether this is a single-valued type.- Returns:
true
if this is single-valued,false
otherwise.
-
map
Maps the underlying value to a different component type.- Type Parameters:
U
- The new component type- Parameters:
mapper
- A mapper- Returns:
- A new value
-
peek
Performs the givenaction
on the first element if this is an eager implementation. Performs the givenaction
on all elements (the first immediately, successive deferred), if this is a lazy implementation.- Parameters:
action
- The action that will be performed on the element(s).- Returns:
- this instance
-
stringPrefix
String stringPrefix()Returns the name of this Value type, which is used by toString().- Returns:
- This type name.
-
out
Sends the string representations of this to thePrintStream
. If this value consists of multiple elements, each element is displayed in a new line.- Parameters:
out
- The PrintStream to write to- Throws:
IllegalStateException
- ifPrintStream.checkError()
is true after writing to stream.
-
out
Sends the string representations of this to thePrintWriter
. If this value consists of multiple elements, each element is displayed in a new line.- Parameters:
writer
- The PrintWriter to write to- Throws:
IllegalStateException
- ifPrintWriter.checkError()
is true after writing to writer.
-
stderr
@GwtIncompatible("java.io.PrintStream is not implemented") default void stderr()Sends the string representations of this to the standard error stream System.err. If this value consists of multiple elements, each element is displayed in a new line.- Throws:
IllegalStateException
- ifPrintStream.checkError()
is true after writing to stderr.
-
stdout
@GwtIncompatible("java.io.PrintStream is not implemented") default void stdout()Sends the string representations of this to the standard output stream System.out. If this value consists of multiple elements, each element is displayed in a new line.- Throws:
IllegalStateException
- ifPrintStream.checkError()
is true after writing to stdout.
-
iterator
Returns a richio.vavr.collection.Iterator
. -
toArray
Converts this to aArray
.- Returns:
- A new
Array
.
-
toCharSeq
Converts this to aCharSeq
.- Returns:
- A new
CharSeq
.
-
toCompletableFuture
Converts this to aCompletableFuture
- Returns:
- A new
CompletableFuture
containing the value
-
toInvalid
Deprecated.UsetoValidation(Object)
instead.Converts this to aValidation
.- Type Parameters:
U
- value type of aValid
- Parameters:
value
- An instance of aValid
value- Returns:
- A new
Validation.Valid
containing the givenvalue
if this is empty, otherwise a newValidation.Invalid
containing this value.
-
toInvalid
Deprecated.UsetoValidation(Supplier)
instead.Converts this to aValidation
.- Type Parameters:
U
- value type of aValid
- Parameters:
valueSupplier
- A supplier of aValid
value- Returns:
- A new
Validation.Valid
containing the result ofvalueSupplier
if this is empty, otherwise a newValidation.Invalid
containing this value. - Throws:
NullPointerException
- ifvalueSupplier
is null
-
toJavaArray
Converts this to a Java array with component typeObject
// = [] of type Object[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray() // = [ok] of type Object[] Try.of(() -> "ok") .toJavaArray() // = [1, 2, 3] of type Object[] List.of(1, 2, 3) .toJavaArray()
- Returns:
- A new Java array.
-
toJavaArray
@Deprecated @GwtIncompatible("reflection is not supported") default T[] toJavaArray(Class<T> componentType) Deprecated.UsetoJavaArray(IntFunction)
insteadConverts this to a Java array having an accurate component type.// = [] of type String[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray(String.class) // = [ok] of type String[] Try.of(() -> "ok") .toJavaArray(String.class) // = [1, 2, 3] of type Integer[] List.of(1, 2, 3) .toJavaArray(Integer.class)
- Parameters:
componentType
- Component type of the array- Returns:
- A new Java array.
- Throws:
NullPointerException
- if componentType is null
-
toJavaArray
Converts this to a Java array having an accurate component type.// = [] of type String[] Future.<String> of(() -> { throw new Error(); }) .toJavaArray(String[]::new) // = [ok] of type String[] Try.of(() -> "ok") .toJavaArray(String[]::new) // = [1, 2, 3] of type Integer[] List.of(1, 2, 3) .toJavaArray(Integer[]::new)
- Parameters:
arrayFactory
- anint
argument function that creates an array of the correct component type with the specified size- Returns:
- The array provided by the factory filled with the values from this
Value
. - Throws:
NullPointerException
- if componentType is null
-
toJavaCollection
Converts this to a specific mutableCollection
of typeC
. Elements are added by callingCollection.add(Object)
.// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaCollection(java.util.HashSet::new) // = [ok] Try.of(() -> "ok") .toJavaCollection(java.util.HashSet::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaCollection(java.util.LinkedHashSet::new)
- Type Parameters:
C
- a sub-type ofjava.util.Collection
- Parameters:
factory
- A factory that returns an empty mutablejava.util.Collection
with the specified initial capacity- Returns:
- a new
java.util.Collection
of typeC
-
toJavaList
Converts this to a mutableList
. Elements are added by callingList.add(Object)
.// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaList() // = [ok] Try.of(() -> "ok") .toJavaList() // = [1, 2, 3] List.of(1, 2, 3) .toJavaList()
- Returns:
- A new
ArrayList
.
-
toJavaList
Converts this to a specific mutableList
. Elements are added by callingList.add(Object)
.// = [] Future.<String> of(() -> { throw new Error(); }) .toJavaList(java.util.ArrayList::new) // = [ok] Try.of(() -> "ok") .toJavaList(java.util.ArrayList::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaList(java.util.ArrayList::new) // = [1, 2, 3] List.of(1, 2, 3) .toJavaList(capacity -> new java.util.LinkedList<>())
- Type Parameters:
LIST
- A sub-type ofjava.util.List
- Parameters:
factory
- A factory that returns an empty mutablejava.util.List
with the specified initial capacity- Returns:
- a new
java.util.List
of typeLIST
-
toJavaMap
Converts this to a mutableMap
. Elements are added by callingMap.put(Object, Object)
.// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(s -> Tuple.of(s, s.length())) // = {ok=2} Try.of(() -> "ok") .toJavaMap(s -> Tuple.of(s, s.length())) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(i -> Tuple.of(i, (char) (i + 64)))
- Type Parameters:
K
- The key typeV
- The value type- Parameters:
f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
HashMap
.
-
toJavaMap
default <K,V, MAP toJavaMapMAP extends Map<K, V>> (Supplier<MAP> factory, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to a specific mutableMap
. Elements are added by callingMap.put(Object, Object)
.// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(java.util.HashMap::new, s -> s, String::length) // = {ok=2} Try.of(() -> "ok") .toJavaMap(java.util.TreeMap::new, s -> s, String::length) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(java.util.TreeMap::new, i -> i, i -> (char) (i + 64))
- Type Parameters:
K
- The key typeV
- The value typeMAP
- a sub-type ofjava.util.Map
- Parameters:
factory
- A factory that creates an empty mutablejava.util.Map
keyMapper
- A function that maps an element to a keyvalueMapper
- A function that maps an element to a value- Returns:
- a new
java.util.Map
of typeMAP
-
toJavaMap
default <K,V, MAP toJavaMapMAP extends Map<K, V>> (Supplier<MAP> factory, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to a specific mutableMap
. Elements are added by callingMap.put(Object, Object)
.// = {} Future.<String> of(() -> { throw new Error(); }) .toJavaMap(java.util.HashMap::new, s -> Tuple.of(s, s.length())) // = {ok=2} Try.of(() -> "ok") .toJavaMap(java.util.TreeMap::new, s -> Tuple.of(s, s.length())) // = {1=A, 2=B, 3=C} List.of(1, 2, 3) .toJavaMap(java.util.TreeMap::new, i -> Tuple.of(i, (char) (i + 64)))
- Type Parameters:
K
- The key typeV
- The value typeMAP
- a sub-type ofjava.util.Map
- Parameters:
factory
- A factory that creates an empty mutablejava.util.Map
f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- a new
java.util.Map
of typeMAP
-
toJavaOptional
Converts this to anOptional
.// = Optional.empty Future.of(() -> { throw new Error(); }) .toJavaOptional() // = Optional[ok] Try.of(() -> "ok") .toJavaOptional() // = Optional[1] List.of(1, 2, 3) .toJavaOptional()
- Returns:
- A new
Optional
.
-
toJavaSet
Converts this to a mutableSet
. Elements are added by callingSet.add(Object)
.// = [] Future.of(() -> { throw new Error(); }) .toJavaSet() // = [ok] Try.of(() -> "ok") .toJavaSet() // = [1, 2, 3] List.of(1, 2, 3) .toJavaSet()
- Returns:
- A new
HashSet
.
-
toJavaSet
Converts this to a specificSet
. Elements are added by callingSet.add(Object)
.// = [] Future.of(() -> { throw new Error(); }) .toJavaSet(java.util.HashSet::new) // = [ok] Try.of(() -> "ok") .toJavaSet(java.util.HashSet::new) // = [3, 2, 1] List.of(1, 2, 3) .toJavaSet(capacity -> new java.util.TreeSet<>(Comparator.reverseOrder()))
- Type Parameters:
SET
- a sub-type ofjava.util.Set
- Parameters:
factory
- A factory that returns an empty mutablejava.util.Set
with the specified initial capacity- Returns:
- a new
java.util.Set
of typeSET
-
toJavaStream
Converts this to a sequentialStream
by callingStreamSupport.stream(this.spliterator(), false)
.// empty Stream Future.of(() -> { throw new Error(); }) .toJavaStream() // Stream containing "ok" Try.of(() -> "ok") .toJavaStream() // Stream containing 1, 2, 3 List.of(1, 2, 3) .toJavaStream()
- Returns:
- A new sequential
Stream
. - See Also:
-
toJavaParallelStream
Converts this to a parallelStream
by callingStreamSupport.stream(this.spliterator(), true)
.// empty Stream Future.of(() -> { throw new Error(); }) .toJavaParallelStream() // Stream containing "ok" Try.of(() -> "ok") .toJavaParallelStream() // Stream containing 1, 2, 3 List.of(1, 2, 3) .toJavaParallelStream()
- Returns:
- A new parallel
Stream
. - See Also:
-
toLeft
Deprecated.UsetoEither(Object)
instead.Converts this to aEither
.- Type Parameters:
R
- right type- Parameters:
right
- An instance of a right value- Returns:
- A new
Either.Right
containing the value ofright
if this is empty, otherwise a newEither.Left
containing this value.
-
toLeft
Deprecated.UsetoEither(Supplier)
instead.Converts this to aEither
.- Type Parameters:
R
- right type- Parameters:
right
- A supplier of a right value- Returns:
- A new
Either.Right
containing the result ofright
if this is empty, otherwise a newEither.Left
containing this value. - Throws:
NullPointerException
- ifright
is null
-
toList
Converts this to aList
.- Returns:
- A new
List
.
-
toMap
default <K,V> Map<K,V> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
keyMapper
- A function that maps an element to a keyvalueMapper
- A function that maps an element to a value- Returns:
- A new
HashMap
.
-
toMap
Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
HashMap
.
-
toLinkedMap
default <K,V> Map<K,V> toLinkedMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
keyMapper
- A function that maps an element to a keyvalueMapper
- A function that maps an element to a value- Returns:
- A new
LinkedHashMap
.
-
toLinkedMap
default <K,V> Map<K,V> toLinkedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
LinkedHashMap
.
-
toSortedMap
default <K extends Comparable<? super K>,V> SortedMap<K,V> toSortedMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
keyMapper
- A function that maps an element to a keyvalueMapper
- A function that maps an element to a value- Returns:
- A new
TreeMap
.
-
toSortedMap
default <K extends Comparable<? super K>,V> SortedMap<K,V> toSortedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
TreeMap
.
-
toSortedMap
default <K,V> SortedMap<K,V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
comparator
- A comparator that induces an order of the Map keys.keyMapper
- A function that maps an element to a keyvalueMapper
- A function that maps an element to a value- Returns:
- A new
TreeMap
.
-
toSortedMap
default <K,V> SortedMap<K,V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) Converts this to aMap
.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
comparator
- A comparator that induces an order of the Map keys.f
- A function that maps an element to a key/value pair represented by Tuple2- Returns:
- A new
TreeMap
.
-
toOption
Converts this to anOption
.- Returns:
- A new
Option
.
-
toEither
Converts this to anEither
. -
toEither
Converts this to anEither
. -
toValidation
Converts this to anValidation
.- Type Parameters:
E
- Validation error component type- Parameters:
invalid
- An invalid value for theValidation
- Returns:
- A new
Validation
.
-
toValidation
Converts this to anValidation
.- Type Parameters:
E
- Validation error component type- Parameters:
invalidSupplier
- ASupplier
for the invalid value for theValidation
- Returns:
- A new
Validation
.
-
toQueue
Converts this to aQueue
.- Returns:
- A new
Queue
.
-
toPriorityQueue
Converts this to aPriorityQueue
.- Returns:
- A new
PriorityQueue
.
-
toPriorityQueue
Converts this to aPriorityQueue
.- Parameters:
comparator
- A comparator that induces an order of the PriorityQueue elements.- Returns:
- A new
PriorityQueue
.
-
toRight
Deprecated.UsetoEither(Object)
instead.Converts this to aEither
.- Type Parameters:
L
- left type- Parameters:
left
- An instance of a left value- Returns:
- A new
Either.Left
containing the value ofleft
if this is empty, otherwise a newEither.Right
containing this value.
-
toRight
Deprecated.UsetoEither(Supplier)
instead.Converts this to aEither
.- Type Parameters:
L
- left type- Parameters:
left
- A supplier of a left value- Returns:
- A new
Either.Left
containing the result ofleft
if this is empty, otherwise a newEither.Right
containing this value. - Throws:
NullPointerException
- ifleft
is null
-
toSet
Converts this to aSet
.- Returns:
- A new
HashSet
.
-
toLinkedSet
Converts this to aSet
.- Returns:
- A new
LinkedHashSet
.
-
toSortedSet
Converts this to aSortedSet
. Current items must be comparable- Returns:
- A new
TreeSet
. - Throws:
ClassCastException
- if items are not comparable
-
toSortedSet
Converts this to aSortedSet
.- Parameters:
comparator
- A comparator that induces an order of the SortedSet elements.- Returns:
- A new
TreeSet
.
-
toStream
Converts this to aStream
.- Returns:
- A new
Stream
.
-
toTry
Converts this to aTry
.If this value is undefined, i.e. empty, then a new
Failure(NoSuchElementException)
is returned, otherwise a newSuccess(value)
is returned.- Returns:
- A new
Try
.
-
toTry
Converts this to aTry
.If this value is undefined, i.e. empty, then a new
Failure(ifEmpty.get())
is returned, otherwise a newSuccess(value)
is returned.- Parameters:
ifEmpty
- an exception supplier- Returns:
- A new
Try
.
-
toTree
Converts this to aTree
.- Returns:
- A new
Tree
.
-
toTree
default <ID> List<Tree.Node<T>> toTree(Function<? super T, ? extends ID> idMapper, Function<? super T, ? extends ID> parentMapper) - Type Parameters:
ID
- Id type- Parameters:
idMapper
- A mapper from source item to unique identifier of that itemparentMapper
- A mapper from source item to unique identifier of parent item. Need return null for root items- Returns:
- A new
Tree
. - See Also:
-
toValid
Deprecated.UsetoValidation(Object)
instead.Converts this to aValidation
.- Type Parameters:
E
- error type of anInvalid
- Parameters:
error
- An error- Returns:
- A new
Validation.Invalid
containing the givenerror
if this is empty, otherwise a newValidation.Valid
containing this value.
-
toValid
Deprecated.UsetoValidation(Supplier)
instead.Converts this to aValidation
.- Type Parameters:
E
- error type of anInvalid
- Parameters:
errorSupplier
- A supplier of an error- Returns:
- A new
Validation.Invalid
containing the result oferrorSupplier
if this is empty, otherwise a newValidation.Valid
containing this value. - Throws:
NullPointerException
- ifvalueSupplier
is null
-
toVector
Converts this to aVector
.- Returns:
- A new
Vector
.
-
spliterator
- Specified by:
spliterator
in interfaceIterable<T>
-
equals
Clarifies that values have a proper equals() method implemented. -
hashCode
int hashCode()Clarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
String toString()Clarifies that values have a proper toString() method implemented.See Object.toString().
-
toValidation(Supplier)
instead.