Streams¶
- public class org.apache.commons.lang3.stream.Streams[source]¶
Provides utility functions, and classes for working with the
java.util.streampackage, or more generally, with Java 8 lambdas. More specifically, it attempts to address the fact that lambdas are supposed not to throw Exceptions, at least not checked Exceptions, AKA instances ofException. This enforces the use of constructs like:Consumer<java.lang.reflect.Method> consumer = m -> { try { m.invoke(o, args); } catch (Throwable t) { throw Failable.rethrow(t); } }; stream.forEach(consumer);
Using a
FailableStream, this can be rewritten as follows:Streams.failable(stream).forEach(m -> m.invoke(o, args));
Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than in the first version.
Constructors¶
Deprecated. Constructs a new instance. |
- @Deprecated public Streams()[source]¶
Deprecated: Will be private in 4.0.0.
Constructs a new instance.
Methods¶
Shorthand for |
|
Shorthand for |
|
Converts the given |
|
Converts the given |
|
Streams only instances of the give Class in a collection. |
|
Streams the non-null element. |
|
Streams the non-null elements of an array. |
|
Streams the non-null elements of a collection. |
|
Streams the non-null elements of a stream. |
|
Creates a sequential stream on the given Iterable. |
|
Null-safe version of |
|
Delegates to |
|
Streams the elements of the given enumeration in order. |
|
Creates a sequential stream on the given Iterator. |
|
Deprecated. Converts the given |
|
Deprecated. Converts the given |
|
Returns a |
- public static <T> Streams.FailableStream<T> failableStream(T value)[source]¶
Shorthand for
Streams.failableStream(value == null ? Stream.empty() : Stream.of(value)).- Type parameters:
<T> – The type of stream elements.
- Parameters:
value – The single element of the new stream, may be
null.- Returns:
The new FailableStream on
valueor an empty stream.- Since:
3.15.0
- @SafeVarargs public static <T> Streams.FailableStream<T> failableStream(T... values)[source]¶
Shorthand for
Streams.failableStream(Streams.of(arrayValues)).- Type parameters:
<T> – The type of stream elements.
- Parameters:
values – The elements of the new stream, may be
null.- Returns:
The new FailableStream on
valuesor an empty stream.- Since:
3.14.0
- public static <T> Streams.FailableStream<T> failableStream(Collection<T> stream)[source]¶
Converts the given
Collectioninto aFailableStream. This is basically a simplified, reduced version of theStreamclass, with the same underlying element stream, except that failable objects, likeFailablePredicate,FailableFunction, orFailableConsumermay be applied, instead ofPredicate,Function, orConsumer. The idea is to rewrite a code snippet like this:final List<O> list; final Method m; final Function<O, String> mapper = (o) -> { try { return (String) m.invoke(o); } catch (Throwable t) { throw Failable.rethrow(t); } }; final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list; final Method m; final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional, intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas better than the first version.
- Type parameters:
<T> – The streams element type.
- Parameters:
stream – The stream, which is being converted.
- Returns:
The
FailableStream, which has been created by converting the stream.- Since:
3.13.0
- public static <T> Streams.FailableStream<T> failableStream(Stream<T> stream)[source]¶
Converts the given
streaminto aFailableStream. This is basically a simplified, reduced version of theStreamclass, with the same underlying element stream, except that failable objects, likeFailablePredicate,FailableFunction, orFailableConsumermay be applied, instead ofPredicate,Function, orConsumer. The idea is to rewrite a code snippet like this:final List<O> list; final Method m; final Function<O, String> mapper = (o) -> { try { return (String) m.invoke(o); } catch (Throwable t) { throw Failable.rethrow(t); } }; final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list; final Method m; final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional, intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas better than the first version.
- Type parameters:
<T> – The streams element type.
- Parameters:
stream – The stream, which is being converted.
- Returns:
The
FailableStream, which has been created by converting the stream.- Since:
3.13.0
- public static <E> Stream<E> instancesOf(Class<? super E> clazz, Collection<? super E> collection)[source]¶
Streams only instances of the give Class in a collection.
This method shorthand for:
(Stream<E>) Streams.toStream(collection).filter(collection, SomeClass.class::isInstance);
- Type parameters:
<E> – The type of elements in the collection we want to stream.
- Parameters:
clazz – The type of elements in the collection we want to stream.
collection – The collection to stream or null.
- Returns:
A non-null stream that only provides instances we want.
- Since:
3.13.0
- public static <E> Stream<E> nonNull(E array)[source]¶
Streams the non-null element.
- Type parameters:
<E> – The type of elements in the collection.
- Parameters:
array – The element to stream or null.
- Returns:
A non-null stream that filters out a null element.
- Since:
3.15.0
- @SafeVarargs public static <E> Stream<E> nonNull(E... array)[source]¶
Streams the non-null elements of an array.
- Type parameters:
<E> – The type of elements in the collection.
- Parameters:
array – The array to stream or null.
- Returns:
A non-null stream that filters out null elements.
- Since:
3.13.0
- public static <E> Stream<E> nonNull(Collection<E> collection)[source]¶
Streams the non-null elements of a collection.
- Type parameters:
<E> – The type of elements in the collection.
- Parameters:
collection – The collection to stream or null.
- Returns:
A non-null stream that filters out null elements.
- Since:
3.13.0
- public static <E> Stream<E> nonNull(Stream<E> stream)[source]¶
Streams the non-null elements of a stream.
- Type parameters:
<E> – The type of elements in the collection.
- Parameters:
stream – The stream to stream or null.
- Returns:
A non-null stream that filters out null elements.
- Since:
3.13.0
- public static <E> Stream<E> of(Iterable<E> iterable)[source]¶
Creates a sequential stream on the given Iterable.
- Type parameters:
<E> – The type of elements in the Iterable.
- Parameters:
iterable – The Iterable to stream or null.
- Returns:
A new Stream or
Stream#empty()if the Iterable is null.- Since:
3.13.0
- @SafeVarargs public static <T> Stream<T> of(T... values)[source]¶
Null-safe version of
Stream#of(Object[]).- Type parameters:
<T> – The type of stream elements.
- Parameters:
values – The elements of the new stream, may be
null.- Returns:
The new stream on
valuesorStream#empty().- Since:
3.13.0
- public static <E> Stream<E> of(Collection<E> collection)[source]¶
Delegates to
Collection#stream()or returnsStream#empty()if the collection is null.- Type parameters:
<E> – The type of elements in the collection.
- Parameters:
collection – The collection to stream or null.
- Returns:
Collection#stream()orStream#empty()if the collection is null.- Since:
3.13.0
- public static <E> Stream<E> of(Enumeration<E> enumeration)[source]¶
Streams the elements of the given enumeration in order.
- Type parameters:
<E> – The enumeration element type.
- Parameters:
enumeration – The enumeration to stream.
- Returns:
A new stream.
- Since:
3.13.0
- public static <E> Stream<E> of(Iterator<E> iterator)[source]¶
Creates a sequential stream on the given Iterator.
- Type parameters:
<E> – The type of elements in the Iterator.
- Parameters:
iterator – The Iterator to stream or null.
- Returns:
A new Stream or
Stream#empty()if the Iterator is null.- Since:
3.13.0
- @Deprecated public static <E> Streams.FailableStream<E> stream(Collection<E> collection)[source]¶
Deprecated: Use
#failableStream(Collection).Converts the given
Collectioninto aFailableStream. This is basically a simplified, reduced version of theStreamclass, with the same underlying element stream, except that failable objects, likeFailablePredicate,FailableFunction, orFailableConsumermay be applied, instead ofPredicate,Function, orConsumer. The idea is to rewrite a code snippet like this:final List<O> list; final Method m; final Function<O, String> mapper = (o) -> { try { return (String) m.invoke(o); } catch (Throwable t) { throw Failable.rethrow(t); } }; final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list; final Method m; final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional, intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas better than the first version.
- Type parameters:
<E> – The streams element type.
- Parameters:
collection – The stream, which is being converted.
- Returns:
The
FailableStream, which has been created by converting the stream.
- @Deprecated public static <T> Streams.FailableStream<T> stream(Stream<T> stream)[source]¶
Deprecated: Use
#failableStream(Stream).Converts the given
streaminto aFailableStream. This is basically a simplified, reduced version of theStreamclass, with the same underlying element stream, except that failable objects, likeFailablePredicate,FailableFunction, orFailableConsumermay be applied, instead ofPredicate,Function, orConsumer. The idea is to rewrite a code snippet like this:final List<O> list; final Method m; final Function<O, String> mapper = (o) -> { try { return (String) m.invoke(o); } catch (Throwable t) { throw Failable.rethrow(t); } }; final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list; final Method m; final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional, intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas better than the first version.
- Type parameters:
<T> – The streams element type.
- Parameters:
stream – The stream, which is being converted.
- Returns:
The
FailableStream, which has been created by converting the stream.
- public static <T> Collector<T, List<T>, T[]> toArray(Class<T> elementType)[source]¶
Returns a
Collectorthat accumulates the input elements into a new array.- Type parameters:
<T> – The type of the input elements
- Parameters:
elementType – Type of an element in the array.
- Returns:
A
Collectorwhich collects all the input elements into an array, in encounter order