Package org.apache.commons.lang3
Class Streams
- java.lang.Object
-
- org.apache.commons.lang3.Streams
-
@Deprecated public class Streams extends java.lang.Object
Deprecated.UseStreams
.Provides utility functions, and classes for working with thejava.util.stream
package, 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 likeConsumer<java.lang.reflect.Method> consumer = m -> { try { m.invoke(o, args); } catch (Throwable t) { throw Functions.rethrow(t); } }; stream.forEach(consumer);
Using aStreams.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.- Since:
- 3.10
- See Also:
Stream
,Functions
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Streams.ArrayCollector<O>
Deprecated.static class
Streams.FailableStream<O>
Deprecated.
-
Constructor Summary
Constructors Constructor Description Streams()
Deprecated.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <O> Streams.FailableStream<O>
stream(java.util.Collection<O> stream)
Deprecated.Converts the givenCollection
into aStreams.FailableStream
.static <O> Streams.FailableStream<O>
stream(java.util.stream.Stream<O> stream)
Deprecated.Converts the givenstream
into aStreams.FailableStream
.static <O> java.util.stream.Collector<O,?,O[]>
toArray(java.lang.Class<O> pElementType)
Deprecated.Returns aCollector
that accumulates the input elements into a new array.
-
-
-
Method Detail
-
stream
public static <O> Streams.FailableStream<O> stream(java.util.stream.Stream<O> stream)
Deprecated.Converts the givenstream
into aStreams.FailableStream
. This is basically a simplified, reduced version of theStream
class, with the same underlying element stream, except that failable objects, likeFunctions.FailablePredicate
,Functions.FailableFunction
, orFunctions.FailableConsumer
may 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 Functions.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 = Functions.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:
O
- The streams element type.- Parameters:
stream
- The stream, which is being converted.- Returns:
- The
Streams.FailableStream
, which has been created by converting the stream.
-
stream
public static <O> Streams.FailableStream<O> stream(java.util.Collection<O> stream)
Deprecated.Converts the givenCollection
into aStreams.FailableStream
. This is basically a simplified, reduced version of theStream
class, with the same underlying element stream, except that failable objects, likeFunctions.FailablePredicate
,Functions.FailableFunction
, orFunctions.FailableConsumer
may 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 Functions.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 = Functions.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:
O
- The streams element type.- Parameters:
stream
- The stream, which is being converted.- Returns:
- The
Streams.FailableStream
, which has been created by converting the stream.
-
toArray
public static <O> java.util.stream.Collector<O,?,O[]> toArray(java.lang.Class<O> pElementType)
Deprecated.Returns aCollector
that accumulates the input elements into a new array.- Type Parameters:
O
- the type of the input elements- Parameters:
pElementType
- Type of an element in the array.- Returns:
- a
Collector
which collects all the input elements into an array, in encounter order
-
-