public class StreamUtils
extends java.lang.Object
| Constructor and Description |
|---|
StreamUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> <any> |
append(<any> stream,
T... values)
Append values to the end of this SequenceM
|
static <T> <any> |
appendStream(<any> stream1,
<any> append)
Append Stream to this SequenceM
|
static <T> <any> |
batchBySize(<any> stream,
int groupSize)
Group elements in a Monad into a Stream
|
static <T,C extends java.util.Collection<T>> |
batchBySize(<any> stream,
int groupSize,
java.util.function.Supplier<C> factory) |
static <T> <any> |
batchBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t) |
static <T,C extends java.util.Collection<T>> |
batchBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t,
java.util.function.Supplier<C> factory) |
static <T> <any> |
batchByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t) |
static <T,C extends java.util.Collection<T>> |
batchByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t,
java.util.function.Supplier<C> factory) |
static <T> <any> |
batchUntil(<any> stream,
java.util.function.Predicate<T> predicate) |
static <T> <any> |
batchWhile(<any> stream,
java.util.function.Predicate<T> predicate) |
static <T,C extends java.util.Collection<T>> |
batchWhile(<any> stream,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<C> factory) |
static <T,U> <any> |
cast(<any> stream,
java.lang.Class<U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException. |
static <T,A,R> <any> |
collect(<any> stream,
<any> collectors)
Apply multiple Collectors, simultaneously to a Stream
|
static <T,A,R> <any> |
collect(<any> stream,
java.lang.Iterable<java.util.stream.Collector> collectors)
Apply multiple Collectors, simultaneously to a Stream
|
static <T> List |
collect(<any> stream,
com.aol.cyclops.sequence.streamable.Streamable<java.util.stream.Collector> collectors)
Apply multiple Collectors, simultaneously to a Stream
|
static <T> <any> |
completableFutureToStream(java.util.concurrent.CompletableFuture<T> future) |
static <U> <any> |
concat(java.lang.Object o,
<any> stream)
Concat an Object and a Stream
If the Object is a Stream, Streamable or Iterable will be converted (or left) in Stream form and concatonated
Otherwise a new Stream.ofAll(o) is created
|
static <U> <any> |
cycle(<any> s)
Create a new Stream that infiniteable cycles the provided Stream
|
static <T> <any> |
cycle(<any> stream,
com.aol.cyclops.sequence.Monoid<T> m,
int times)
Convert to a Stream with the result of a reduction operation repeated
specified times
|
static <U> <any> |
cycle(int times,
com.aol.cyclops.sequence.streamable.Streamable<U> s)
Create a Stream that finitely cycles the provided Streamable, provided number of times
|
static <U> <any> |
cycle(com.aol.cyclops.sequence.streamable.Streamable<U> s)
Create a Stream that infiniteable cycles the provided Streamable
|
static <T> <any> |
cycleUntil(<any> stream,
java.util.function.Predicate<? super T> predicate)
Repeat in a Stream until specified predicate holds
|
static <T> <any> |
cycleWhile(<any> stream,
java.util.function.Predicate<? super T> predicate)
Repeat in a Stream while specified predicate holds
|
static <T> <any> |
debounce(<any> stream,
long time,
java.util.concurrent.TimeUnit t) |
static <T> <any> |
deleteBetween(<any> stream,
int start,
int end)
Delete elements between given indexes in a Stream
|
static <T> <any> |
duplicate(<any> stream)
Duplicate a Stream, buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
static <T> boolean |
endsWith(<any> stream,
java.lang.Iterable<T> iterable) |
static <T> T |
firstValue(<any> stream) |
static <T> <any> |
fixedDelay(<any> stream,
long time,
java.util.concurrent.TimeUnit unit) |
static <T,R> <any> |
flatMapAnyM(<any> stream,
java.util.function.Function<? super T,com.aol.cyclops.monad.AnyM<? extends R>> fn) |
static <T> <any> |
flatMapBufferedReader(<any> stream,
java.util.function.Function<? super T,java.io.BufferedReader> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied BufferedReaders
|
static <T> <any> |
flatMapCharSequence(<any> stream,
java.util.function.Function<? super T,java.lang.CharSequence> fn)
Perform a flatMap operation where the result will be a flattened stream of Characters
from the CharSequence returned by the supplied function.
|
static <T,R> <any> |
flatMapCollection(<any> stream,
java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
flatMap operation that allows a Collection to be returned
|
static <T,R> <any> |
flatMapCompletableFuture(<any> stream,
java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
assertThat(StreamUtils.flatMapCompletableFuture(Stream.ofAll(1,2,3),
i->CompletableFuture.completedFuture(i+2))
.collect(Collectors.toList()),
equalTo(Arrays.asList(3,4,5)));
|
static <T> <any> |
flatMapFile(<any> stream,
java.util.function.Function<? super T,java.io.File> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied files.
|
static <T,R> <any> |
flatMapOptional(<any> stream,
java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
cross type flatMap, removes null entries
|
static <T,R> <any> |
flatMapSequenceM(<any> stream,
java.util.function.Function<? super T,com.aol.cyclops.sequence.SequenceM<? extends R>> fn)
flatMap operation
|
static <T,R> <any> |
flatMapStream(<any> stream,
java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
assertThat(StreamUtils.flatMapStream(Stream.ofAll(1,2,3),
i->Stream.ofAll(i)).collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3)));
|
static <T> <any> |
flatMapURL(<any> stream,
java.util.function.Function<? super T,java.net.URL> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied URLs
|
static <T,R> <any> |
flatten(<any> stream) |
static <T> T |
foldLeft(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer) |
static <T> T |
foldLeftMapToType(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
Attempt to map this Monad to the same type as the supplied Monoid (using mapToType on the monoid interface)
Then use Monoid to reduce values
|
static <T> T |
foldRight(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer) |
static <T> T |
foldRightMapToType(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
Attempt to map this Monad to the same type as the supplied Monoid (using mapToType on the monoid interface)
Then use Monoid to reduce values
|
static <T> com.aol.cyclops.sequence.future.FutureOperations<T> |
futureOperations(<any> stream,
java.util.concurrent.Executor exec) |
static <T> com.aol.cyclops.sequence.HeadAndTail<T> |
headAndTail(<any> stream)
extract head and tail together
|
static <T> java.util.Optional<com.aol.cyclops.sequence.HeadAndTail<T>> |
headAndTailOptional(<any> stream)
Stream<String> helloWorld = Stream.ofAll();
Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld);
assertTrue(!headAndTail.isPresent());
|
static <T> JavaslangHotStream<T> |
hotStream(<any> stream,
java.util.concurrent.Executor exec) |
static <T> <any> |
insertAt(<any> stream,
int pos,
T... values)
Insert data into a stream at given position
|
static <T> <any> |
insertStreamAt(<any> stream1,
int pos,
<any> insert)
Insert a Stream into the middle of this stream at the specified position
|
static <T> <any> |
intersperse(<any> stream,
T value)
Returns a stream with a given value interspersed between any two values
of this stream.
|
static <T> <any> |
jitter(<any> stream,
long jitterInNanos) |
static <T> java.lang.String |
join(<any> stream) |
static <T> java.lang.String |
join(<any> stream,
java.lang.String sep) |
static <T> java.lang.String |
join(<any> stream,
java.lang.String sep,
java.lang.String start,
java.lang.String end) |
static <U> <any> |
limit(<any> stream,
long time,
java.util.concurrent.TimeUnit unit) |
static <U> <any> |
limitLast(<any> stream,
int num) |
static <U> <any> |
limitUntil(<any> stream,
java.util.function.Predicate<? super U> predicate)
Take elements from a Stream until the predicate holds
|
static <U> <any> |
limitWhile(<any> stream,
java.util.function.Predicate<? super U> predicate)
Take elements from a stream while the predicates hold
|
static <T,R> R |
mapReduce(<any> stream,
java.util.function.Function<? super T,? extends R> mapper,
com.aol.cyclops.sequence.Monoid<R> reducer)
Attempt to map this Monad to the same type as the supplied Monoid, using supplied function
Then use Monoid to reduce values
|
static <T,R> R |
mapReduce(<any> stream,
com.aol.cyclops.sequence.Monoid<R> reducer)
Attempt to map this Monad to the same type as the supplied Monoid (using mapToType on the monoid interface)
Then use Monoid to reduce values
|
static <T> java.util.Optional<T> |
max(<any> stream,
java.util.Comparator<? super T> comparator) |
static <T,C extends java.lang.Comparable<? super C>> |
maxBy(<any> stream,
java.util.function.Function<T,C> f) |
static <T> java.util.Optional<T> |
min(<any> stream,
java.util.Comparator<? super T> comparator) |
static <T,C extends java.lang.Comparable<C>> |
minBy(<any> stream,
java.util.function.Function<T,C> f) |
static <T> boolean |
noneMatch(<any> stream,
java.util.function.Predicate<? super T> c)
assertThat(StreamUtils.noneMatch(of(1,2,3,4,5),it-> it==5000),equalTo(true));
|
static <T,U> <any> |
ofType(<any> stream,
java.lang.Class<U> type)
Keep only those elements in a stream that are of a given type.
|
static <T> <any> |
onePer(<any> stream,
long time,
java.util.concurrent.TimeUnit t) |
static <T> <any> |
optionalToStream(java.util.Optional<T> optional) |
static <T> <any> |
partition(<any> stream,
java.util.function.Predicate<T> splitter)
Partition a Stream into two one a per element basis, based on predicate's boolean value
|
static <T> <any> |
prepend(<any> stream,
T... values)
Prepend given values to the start of the Stream
|
static <T> <any> |
prependStream(<any> stream1,
<any> prepend)
Prepend Stream to this SequenceM
|
static <T> <any> |
quadruplicate(<any> stream)
Makes four copies of a Stream
Buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
static <T,EX extends java.lang.Throwable> |
recover(<any> stream,
java.lang.Class<EX> type,
java.util.function.Function<EX,T> fn) |
static <T> <any> |
recover(<any> stream,
java.util.function.Function<java.lang.Throwable,T> fn) |
static <R> <any> |
reduce(<any> stream,
<any> reducers)
Simultanously reduce a stream with multiple reducers
|
static <R> <any> |
reduce(<any> stream,
java.lang.Iterable<com.aol.cyclops.sequence.Monoid<R>> reducers)
Simultaneously reduce a stream with multiple reducers
|
static <U> <any> |
reverse(<any> stream)
Reverse a Stream
|
static <U> <any> |
reversedStream(<any> list)
Create a reversed Stream from a List
|
static <T> <any> |
scanLeft(<any> stream,
com.aol.cyclops.sequence.Monoid<T> monoid)
Scan left using supplied Monoid
|
static <U,T> <any> |
scanRight(<any> stream,
U identity,
java.util.function.BiFunction<? super T,U,U> combiner) |
static <T> com.aol.cyclops.sequence.streamable.Streamable<T> |
shuffle(<any> stream) |
static <U> <any> |
skip(<any> stream,
long time,
java.util.concurrent.TimeUnit unit) |
static <U> <any> |
skipLast(<any> stream,
int num) |
static <U> <any> |
skipUntil(<any> stream,
java.util.function.Predicate<? super U> predicate)
skip elements in Stream until Predicate holds true
|
static <U> <any> |
skipWhile(<any> stream,
java.util.function.Predicate<? super U> predicate)
skip elements in a Stream while Predicate holds true
|
static <T> <any> |
sliding(<any> stream,
int windowSize)
Create a sliding view over this Stream
|
static <T> <any> |
sliding(<any> stream,
int windowSize,
int increment)
Create a sliding view over this Stream
|
static <T> <any> |
splitAt(<any> stream,
int where)
Split at supplied location
|
static <T> <any> |
splitBy(<any> stream,
java.util.function.Predicate<T> splitter)
Split stream at point where predicate no longer holds
|
static <T> boolean |
startsWith(<any> stream,
java.lang.Iterable<T> iterable)
assertTrue(StreamUtils.startsWith(Stream.ofAll(1,2,3,4),Arrays.asList(1,2,3)));
|
static <T> boolean |
startsWith(<any> stream,
java.util.Iterator<T> iterator)
assertTrue(StreamUtils.startsWith(Stream.ofAll(1,2,3,4),Arrays.asList(1,2,3).iterator()))
|
static <K,V> <any> |
stream(<any> it)
Create a stream from a map
|
static <U> <any> |
stream(java.lang.Iterable<U> it)
Create a stream from an iterable
|
static <U> <any> |
stream(java.util.Iterator<U> it)
Create a stream from an iterator
|
static <K,V> <any> |
stream(java.util.Map<K,V> it)
Create a stream from a map
|
static <U> <any> |
stream(java.util.Spliterator<U> it) |
static <T> java.util.concurrent.CompletableFuture<<any>> |
streamToCompletableFuture(<any> stream) |
static <T> java.util.Optional<<any>> |
streamToOptional(<any> stream) |
static <A> <any> |
toBufferingCopier(java.util.Iterator<A> iterator,
int copies) |
static <A> <any> |
toBufferingDuplicator(java.util.Iterator<A> iterator) |
static <A> <any> |
toBufferingDuplicator(java.util.Iterator<A> iterator,
long pos) |
static <A> java.util.Collection<A> |
toConcurrentLazyCollection(<any> stream)
Lazily constructs a Collection from specified Stream.
|
static <T> com.aol.cyclops.sequence.streamable.Streamable<T> |
toConcurrentLazyStreamable(<any> stream) |
static <A> java.util.Collection<A> |
toLazyCollection(<any> stream)
Projects an immutable collection of this stream.
|
static <T> com.aol.cyclops.sequence.streamable.Streamable<T> |
toLazyStreamable(<any> stream) |
static <T> <any> |
toList(<any> stream) |
static <T> <any> |
toSet(<any> stream) |
static <T> com.aol.cyclops.sequence.streamable.Streamable<T> |
toStreamable(<any> stream) |
static <T> <any> |
triplicate(<any> stream)
Triplicates a Stream
Buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
static <T> <any> |
window(<any> stream,
int windowSize,
int increment)
Create a sliding view over this Stream
|
static <T> <any> |
windowBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t) |
static <T> <any> |
windowByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t) |
static <T> <any> |
windowStatefullyWhile(<any> stream,
java.util.function.BiPredicate<com.aol.cyclops.sequence.streamable.Streamable<T>,T> predicate) |
static <T> <any> |
windowWhile(<any> stream,
java.util.function.Predicate<T> predicate) |
static <T> boolean |
xMatch(<any> stream,
int num,
java.util.function.Predicate<? super T> c)
Check that there are specified number of matches of predicate in the Stream
|
static <T> <any> |
xPer(<any> stream,
int x,
long time,
java.util.concurrent.TimeUnit t) |
static <T,S,R> <any> |
zipAnyM(<any> stream,
com.aol.cyclops.monad.AnyM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Generic zip function.
|
static <T,S,R> <any> |
zipSequence(<any> stream,
<any> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Generic zip function.
|
static <T,S,R> <any> |
zipStream(<any> stream,
java.util.stream.BaseStream<? extends S,? extends java.util.stream.BaseStream<? extends S,?>> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Zip this Monad with a Stream
|
public static final <T> java.util.Optional<<any>> streamToOptional(<any> stream)
public static final <T> <any> optionalToStream(java.util.Optional<T> optional)
public static final <T> java.util.concurrent.CompletableFuture<<any>> streamToCompletableFuture(<any> stream)
public static final <T> <any> completableFutureToStream(java.util.concurrent.CompletableFuture<T> future)
public static final <T> <any> splitAt(<any> stream,
int where)
StreamUtils.splitAt(Stream.ofAll(1,2,3))
//Stream[1], Stream[2,3]
public static final <T> <any> splitBy(<any> stream,
java.util.function.Predicate<T> splitter)
StreamUtils.splitBy(Stream.ofAll(1, 2, 3, 4, 5, 6),i->i<4);
//Stream[1,2,3] Stream[4,5,6]
public static final <T> <any> partition(<any> stream,
java.util.function.Predicate<T> splitter)
StreamUtils.partition(Stream.ofAll(1, 2, 3, 4, 5, 6),i -> i % 2 != 0)
//Stream[1,3,5], Stream[2,4,6]
public static final <T> <any> duplicate(<any> stream)
Tuple2<Stream<Integer>, Stream<Integer>> copies =of(1,2,3,4,5,6).duplicate();
assertTrue(copies._1.anyMatch(i->i==2));
assertTrue(copies._2.anyMatch(i->i==2));
public static final <T> <any> triplicate(<any> stream)
Tuple3<Stream<Tuple3<T1,T2,T3>>,Stream<Tuple3<T1,T2,T3>>,Stream<Tuple3<T1,T2,T3>>> Tuple3 = sequence.triplicate();
public static final <T> <any> quadruplicate(<any> stream)
Tuple4<Stream<Tuple4<T1,T2,T3,T4>>,Stream<Tuple4<T1,T2,T3,T4>>,Stream<Tuple4<T1,T2,T3,T4>>,Stream<Tuple4<T1,T2,T3,T4>>> quad = sequence.quadruplicate();
public static final <T> <any> appendStream(<any> stream1,
<any> append)
List<String> result = of(1,2,3).appendStream(of(100,200,300))
.map(it ->it+"!!")
.collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","3!!","100!!","200!!","300!!")));
stream - to appendpublic static final <T> <any> prependStream(<any> stream1,
<any> prepend)
List<String> result = of(1,2,3).prependStream(of(100,200,300))
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("100!!","200!!","300!!","1!!","2!!","3!!")));
stream - to Prependpublic static final <T> <any> append(<any> stream,
T... values)
List<String> result = of(1,2,3).append(100,200,300)
.map(it ->it+"!!")
.collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","3!!","100!!","200!!","300!!")));
values - to appendpublic static final <T> <any> prepend(<any> stream,
T... values)
List<String> result = of(1,2,3).prepend(100,200,300)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("100!!","200!!","300!!","1!!","2!!","3!!")));
values - to prependpublic static final <T> <any> insertAt(<any> stream,
int pos,
T... values)
List<String> result = of(1,2,3).insertAt(1,100,200,300)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","100!!","200!!","300!!","2!!","3!!")));
pos - to insert data atvalues - to insertpublic static final <T> <any> deleteBetween(<any> stream,
int start,
int end)
List<String> result = of(1,2,3,4,5,6).deleteBetween(2,4)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","5!!","6!!")));
start - indexend - indexpublic static final <T> <any> insertStreamAt(<any> stream1,
int pos,
<any> insert)
List<String> result = of(1,2,3).insertStreamAt(1,of(100,200,300))
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","100!!","200!!","300!!","2!!","3!!")));
pos - to insert Stream atstream - to insertpublic static final <T> <any> cycle(<any> stream,
com.aol.cyclops.sequence.Monoid<T> m,
int times)
List<Integer> list = StreamUtils.cycle(Stream.ofAll(1,2,2),Reducers.toCountInt(),3)
.
.collect(Collectors.toList());
//is asList(3,3,3);
m - Monoid to be used in reductiontimes - Number of times value should be repeatedpublic static final <T> com.aol.cyclops.sequence.HeadAndTail<T> headAndTail(<any> stream)
Stream<String> helloWorld = Stream.ofAll("hello","world","last");
HeadAndTail<String> headAndTail = StreamUtils.headAndTail(helloWorld);
String head = headAndTail.head();
assertThat(head,equalTo("hello"));
Stream<String> tail = headAndTail.tail();
assertThat(tail.headAndTail().head(),equalTo("world"));
public static final <T> java.util.Optional<com.aol.cyclops.sequence.HeadAndTail<T>> headAndTailOptional(<any> stream)
Stream<String> helloWorld = Stream.ofAll();
Optional<HeadAndTail<String>> headAndTail = StreamUtils.headAndTailOptional(helloWorld);
assertTrue(!headAndTail.isPresent());
stream - to extract head and tail frompublic static <U> <any> skipUntil(<any> stream,
java.util.function.Predicate<? super U> predicate)
StreamUtils.skipUntil(Stream.ofAll(4,3,6,7),i->i==6).collect(Collectors.toList())
// [6,7]
stream - Stream to skip elements frompredicate - to applypublic static <U> <any> skipLast(<any> stream,
int num)
public static <U> <any> limitLast(<any> stream,
int num)
public static <T> <any> recover(<any> stream,
java.util.function.Function<java.lang.Throwable,T> fn)
public static <T,EX extends java.lang.Throwable> <any> recover(<any> stream,
java.lang.Class<EX> type,
java.util.function.Function<EX,T> fn)
public static <U> <any> skipWhile(<any> stream,
java.util.function.Predicate<? super U> predicate)
StreamUtils.skipWhile(Stream.ofAll(4,3,6,7).sorted(),i->i<6).collect(Collectors.toList())
// [6,7]
stream - predicate - public static <U> <any> limit(<any> stream,
long time,
java.util.concurrent.TimeUnit unit)
public static <U> <any> skip(<any> stream,
long time,
java.util.concurrent.TimeUnit unit)
public static <U> <any> limitWhile(<any> stream,
java.util.function.Predicate<? super U> predicate)
StreamUtils.limitWhile(Stream.ofAll(4,3,6,7).sorted(),i->i<6).collect(Collectors.toList());
//[4,3]
stream - predicate - public static <U> <any> limitUntil(<any> stream,
java.util.function.Predicate<? super U> predicate)
StreamUtils.limitUntil(Stream.ofAll(4,3,6,7),i->i==6).collect(Collectors.toList());
//[4,3]
stream - predicate - public static <U> <any> reverse(<any> stream)
assertThat(StreamUtils.reverse(Stream.ofAll(1,2,3)).collect(Collectors.toList())
,equalTo(Arrays.asList(3,2,1)));
stream - Stream to reversepublic static <U> <any> reversedStream(<any> list)
StreamUtils.reversedStream(asList(1,2,3))
.map(i->i*100)
.forEach(System.out::println);
assertThat(StreamUtils.reversedStream(Arrays.asList(1,2,3)).collect(Collectors.toList())
,equalTo(Arrays.asList(3,2,1)));
list - List to create a reversed Stream frompublic static <U> <any> cycle(<any> s)
assertThat(StreamUtils.cycle(Stream.ofAll(1,2,3))
.limit(6)
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3,1,2,3)));
s - Stream to cyclepublic static <U> <any> cycle(com.aol.cyclops.sequence.streamable.Streamable<U> s)
s - Streamable to cyclepublic static <U> <any> cycle(int times,
com.aol.cyclops.sequence.streamable.Streamable<U> s)
assertThat(StreamUtils.cycle(3,Streamable.of(1,2,2))
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,2,1,2,2,1,2,2)));
s - Streamable to cyclepublic static <U> <any> stream(java.lang.Iterable<U> it)
assertThat(StreamUtils.stream(Arrays.asList(1,2,3))
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3)));
it - Iterable to convert to a Streampublic static <U> <any> stream(java.util.Spliterator<U> it)
public static <U> <any> stream(java.util.Iterator<U> it)
assertThat(StreamUtils.stream(Arrays.asList(1,2,3).iterator())
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3)));
it - Iterator to convert to a Streampublic static <U> <any> concat(java.lang.Object o,
<any> stream)
o - Object to concatstream - Stream to concatpublic static final <K,V> <any> stream(java.util.Map<K,V> it)
Map<String,String> map = new HashMap<>();
map.put("hello","world");
assertThat(StreamUtils.stream(map).collect(Collectors.toList()),equalTo(Arrays.asList(new AbstractMap.SimpleEntry("hello","world"))));
it - Iterator to convert to a Streampublic static final <K,V> <any> stream(<any> it)
HashMap<String,String> map = HashMap.<String,String>empty().put("hello","world");
assertThat(StreamUtils.stream(map).collect(Collectors.toList()),equalTo(Arrays.asList(new AbstractMap.SimpleEntry("hello","world"))));
it - Iterator to convert to a Streampublic static final <T> com.aol.cyclops.sequence.future.FutureOperations<T> futureOperations(<any> stream,
java.util.concurrent.Executor exec)
public static final <T> T firstValue(<any> stream)
public static <R> <any> reduce(<any> stream,
java.lang.Iterable<com.aol.cyclops.sequence.Monoid<R>> reducers)
Monoid<Integer> sum = Monoid.of(0,(a,b)->a+b);
Monoid<Integer> mult = Monoid.of(1,(a,b)->a*b);
val result = StreamUtils.reduce(Stream.ofAll(1,2,3,4),Arrays.asList(sum,mult));
assertThat(result,equalTo(Arrays.asList(10,24)));
stream - Stream to reducereducers - Reducers to reduce Streampublic static <R> <any> reduce(<any> stream,
<any> reducers)
Monoid<String> concat = Monoid.of("",(a,b)->a+b);
Monoid<String> join = Monoid.of("",(a,b)->a+","+b);
assertThat(StreamUtils.reduce(Stream.ofAll("hello", "world", "woo!"),Stream.ofAll(concat,join))
,equalTo(Arrays.asList("helloworldwoo!",",hello,world,woo!")));
stream - Stream to reducereducers - Reducers to reduce Streampublic static <T,A,R> <any> collect(<any> stream,
<any> collectors)
List result = StreamUtils.collect(Stream.ofAll(1,2,3),
Stream.ofAll(Collectors.toList(),
Collectors.summingInt(Integer::intValue),
Collectors.averagingInt(Integer::intValue)));
assertThat(result.get(0),equalTo(Arrays.asList(1,2,3)));
assertThat(result.get(1),equalTo(6));
assertThat(result.get(2),equalTo(2.0));
stream - Stream to collectcollectors - Collectors to applypublic static <T,A,R> <any> collect(<any> stream,
java.lang.Iterable<java.util.stream.Collector> collectors)
List result = StreamUtils.collect(Stream.ofAll(1,2,3),
Arrays.asList(Collectors.toList(),
Collectors.summingInt(Integer::intValue),
Collectors.averagingInt(Integer::intValue)));
assertThat(result.get(0),equalTo(Arrays.asList(1,2,3)));
assertThat(result.get(1),equalTo(6));
assertThat(result.get(2),equalTo(2.0));
stream - Stream to collectcollectors - Collectors to applypublic static <T> List collect(<any> stream,
com.aol.cyclops.sequence.streamable.Streamable<java.util.stream.Collector> collectors)
List result = StreamUtils.collect(Stream.ofAll(1,2,3),
Streamable.<Collector>of(Collectors.toList(),
Collectors.summingInt(Integer::intValue),
Collectors.averagingInt(Integer::intValue)));
assertThat(result.get(0),equalTo(Arrays.asList(1,2,3)));
assertThat(result.get(1),equalTo(6));
assertThat(result.get(2),equalTo(2.0));
stream - Stream to collectcollectors - Collectors to applypublic static final <T> <any> cycleWhile(<any> stream,
java.util.function.Predicate<? super T> predicate)
int count =0;
assertThat(StreamUtils.cycleWhile(Stream.ofAll(1,2,2)
,next -> count++<6 )
.collect(Collectors.toList()),equalTo(Arrays.asList(1,2,2,1,2,2)));
predicate - repeat while truepublic static final <T> <any> cycleUntil(<any> stream,
java.util.function.Predicate<? super T> predicate)
count =0;
assertThat(StreamUtils.cycleUntil(Stream.ofAll(1,2,2,3)
,next -> count++>10 )
.collect(Collectors.toList()),equalTo(Arrays.asList(1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2)));
predicate - repeat while truepublic static final <T,S,R> <any> zipSequence(<any> stream,
<any> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Stream<List<Integer>> zipped = StreamUtils.zip(Stream.ofAll(1,2,3)
,SequenceM.of(2,3,4),
(a,b) -> Arrays.asList(a,b));
List<Integer> zip = zipped.collect(Collectors.toList()).get(1);
assertThat(zip.get(0),equalTo(2));
assertThat(zip.get(1),equalTo(3));
second - Monad to zip withzipper - Zipping functionpublic static final <T,S,R> <any> zipAnyM(<any> stream,
com.aol.cyclops.monad.AnyM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Stream<List<Integer>> zipped = StreamUtils.zip(Stream.ofAll(1,2,3)
,anyM(Optional.of(2)),
(a,b) -> Arrays.asList(a,b));
List<Integer> zip = zipped.collect(Collectors.toList()).get(0);
assertThat(zip.get(0),equalTo(1));
assertThat(zip.get(1),equalTo(2));
public static final <T,S,R> <any> zipStream(<any> stream,
java.util.stream.BaseStream<? extends S,? extends java.util.stream.BaseStream<? extends S,?>> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Stream<List<Integer>> zipped = StreamUtils.zipStream(Stream.ofAll(1,2,3)
,Stream.ofAll(2,3,4),
(a,b) -> Arrays.asList(a,b));
List<Integer> zip = zipped.collect(Collectors.toList()).get(1);
assertThat(zip.get(0),equalTo(2));
assertThat(zip.get(1),equalTo(3));
second - Stream to zip withzipper - Zip funcitonpublic static final <T> <any> sliding(<any> stream,
int windowSize,
int increment)
List<List<Integer>> list = StreamUtils.sliding(Stream.ofAll(1,2,3,4,5,6)
,2,1)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2));
assertThat(list.get(1),hasItems(2,3));
windowSize - Size of sliding windowpublic static final <T> <any> window(<any> stream,
int windowSize,
int increment)
List<List<Integer>> list = StreamUtils.sliding(Stream.ofAll(1,2,3,4,5,6)
,2,1)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2));
assertThat(list.get(1),hasItems(2,3));
windowSize - Size of sliding windowpublic static final <T> <any> sliding(<any> stream,
int windowSize)
List<List<Integer>> list = StreamUtils.sliding(Stream.ofAll(1,2,3,4,5,6)
,2)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2));
assertThat(list.get(1),hasItems(2,3));
stream - Stream to create sliding view onwindowSize - size of windowpublic static final <T> <any> batchBySize(<any> stream,
int groupSize)
List<List<Integer>> list = StreamUtils.grouped(Stream.ofAll(1,2,3,4,5,6)
,3)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2,3));
assertThat(list.get(1),hasItems(4,5,6));
groupSize - Size of each Grouppublic static final <T,C extends java.util.Collection<T>> <any> batchBySize(<any> stream,
int groupSize,
java.util.function.Supplier<C> factory)
public static final <T> com.aol.cyclops.sequence.streamable.Streamable<T> shuffle(<any> stream)
public static final <T> com.aol.cyclops.sequence.streamable.Streamable<T> toLazyStreamable(<any> stream)
public static final <T> com.aol.cyclops.sequence.streamable.Streamable<T> toConcurrentLazyStreamable(<any> stream)
public static final <U,T> <any> scanRight(<any> stream,
U identity,
java.util.function.BiFunction<? super T,U,U> combiner)
public static final <T> <any> scanLeft(<any> stream,
com.aol.cyclops.sequence.Monoid<T> monoid)
assertEquals(asList("", "a", "ab", "abc"),
StreamUtils.scanLeft(Stream.ofAll("a", "b", "c"),Reducers.toString(""))
.collect(Collectors.toList());
monoid - public static <T> boolean xMatch(<any> stream,
int num,
java.util.function.Predicate<? super T> c)
assertTrue(StreamUtils.xMatch(Stream.ofAll(1,2,3,5,6,7),3, i->i>4));
public static final <T> boolean noneMatch(<any> stream,
java.util.function.Predicate<? super T> c)
assertThat(StreamUtils.noneMatch(of(1,2,3,4,5),it-> it==5000),equalTo(true));
public static final <T> java.lang.String join(<any> stream)
public static final <T> java.lang.String join(<any> stream,
java.lang.String sep)
public static final <T> java.lang.String join(<any> stream,
java.lang.String sep,
java.lang.String start,
java.lang.String end)
public static final <T,C extends java.lang.Comparable<C>> java.util.Optional<T> minBy(<any> stream,
java.util.function.Function<T,C> f)
public static final <T> java.util.Optional<T> min(<any> stream,
java.util.Comparator<? super T> comparator)
public static final <T,C extends java.lang.Comparable<? super C>> java.util.Optional<T> maxBy(<any> stream,
java.util.function.Function<T,C> f)
public static final <T> java.util.Optional<T> max(<any> stream,
java.util.Comparator<? super T> comparator)
public static final <T,R> R mapReduce(<any> stream,
com.aol.cyclops.sequence.Monoid<R> reducer)
reducer - Monoid to reduce valuespublic static final <T,R> R mapReduce(<any> stream,
java.util.function.Function<? super T,? extends R> mapper,
com.aol.cyclops.sequence.Monoid<R> reducer)
mapper - Function to map Monad typereducer - Monoid to reduce valuespublic static final <T> T foldLeft(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
reducer - Use supplied Monoid to reduce values starting via foldLeftpublic static final <T> T foldLeftMapToType(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
reducer - Monoid to reduce valuespublic static final <T> T foldRight(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
reducer - Use supplied Monoid to reduce values starting via foldRightpublic static final <T> T foldRightMapToType(<any> stream,
com.aol.cyclops.sequence.Monoid<T> reducer)
reducer - Monoid to reduce valuespublic static final <T> com.aol.cyclops.sequence.streamable.Streamable<T> toStreamable(<any> stream)
public static final <T> <any> toSet(<any> stream)
public static final <T> <any> toList(<any> stream)
public static final <T> boolean startsWith(<any> stream,
java.lang.Iterable<T> iterable)
assertTrue(StreamUtils.startsWith(Stream.ofAll(1,2,3,4),Arrays.asList(1,2,3)));
iterable - public static final <T> boolean endsWith(<any> stream,
java.lang.Iterable<T> iterable)
public static final <T> boolean startsWith(<any> stream,
java.util.Iterator<T> iterator)
assertTrue(StreamUtils.startsWith(Stream.ofAll(1,2,3,4),Arrays.asList(1,2,3).iterator()))
iterator - public static <T> <any> intersperse(<any> stream,
T value)
assertThat(Arrays.asList(1, 0, 2, 0, 3, 0, 4),
equalTo( StreamUtils.intersperse(Stream.ofAll(1, 2, 3, 4),0));
public static <T,U> <any> ofType(<any> stream,
java.lang.Class<U> type)
public static <T,U> <any> cast(<any> stream,
java.lang.Class<U> type)
ClassCastException.
StreamUtils.cast(Stream.ofAll(1, "a", 2, "b", 3),Integer.class)
// throws ClassCastException
public static final <T,R> <any> flatMapSequenceM(<any> stream,
java.util.function.Function<? super T,com.aol.cyclops.sequence.SequenceM<? extends R>> fn)
assertThat(StreamUtils.flatMapSequenceM(Stream.ofAll(1,2,3),
i->SequenceM.of(i+2)).collect(Collectors.toList()),
equalTo(Arrays.asList(3,4,5)));
fn - public static final <T,R> <any> flatMapAnyM(<any> stream,
java.util.function.Function<? super T,com.aol.cyclops.monad.AnyM<? extends R>> fn)
public static final <T,R> <any> flatMapCollection(<any> stream,
java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
assertThat(StreamUtils.flatMapCollection(Stream.ofAll(20),i->Arrays.asList(1,2,i))
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,20)));
public static final <T,R> <any> flatMapStream(<any> stream,
java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
assertThat(StreamUtils.flatMapStream(Stream.ofAll(1,2,3),
i->Stream.ofAll(i)).collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3)));
public static final <T,R> <any> flatMapOptional(<any> stream,
java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
assertThat(StreamUtils.flatMapOptional(Stream.ofAll(1,2,3,null),
Optional::ofNullable)
.collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,3)));
public static final <T,R> <any> flatten(<any> stream)
public static final <T,R> <any> flatMapCompletableFuture(<any> stream,
java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
assertThat(StreamUtils.flatMapCompletableFuture(Stream.ofAll(1,2,3),
i->CompletableFuture.completedFuture(i+2))
.collect(Collectors.toList()),
equalTo(Arrays.asList(3,4,5)));
public static final <T> <any> flatMapCharSequence(<any> stream,
java.util.function.Function<? super T,java.lang.CharSequence> fn)
List<Character> result = StreamUtils.liftAndBindCharSequence(Stream.ofAll("input.file"),
.i->"hello world")
.toList();
assertThat(result,equalTo(Arrays.asList('h','e','l','l','o',' ','w','o','r','l','d')));
fn - public static final <T> <any> flatMapFile(<any> stream,
java.util.function.Function<? super T,java.io.File> fn)
List<String> result = StreamUtils.liftAndBindFile(Stream.ofAll("input.file")
.map(getClass().getClassLoader()::getResource)
.peek(System.out::println)
.map(URL::getFile)
,File::new)
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public static final <T> <any> flatMapURL(<any> stream,
java.util.function.Function<? super T,java.net.URL> fn)
List<String> result = StreamUtils.liftAndBindURL(Stream.ofAll("input.file")
,getClass().getClassLoader()::getResource)
.collect(Collectors.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public static final <T> <any> flatMapBufferedReader(<any> stream,
java.util.function.Function<? super T,java.io.BufferedReader> fn)
Listresult = StreamUtils.liftAndBindBufferedReader(Stream.ofAll("input.file") .map(getClass().getClassLoader()::getResourceAsStream) .map(InputStreamReader::new) ,BufferedReader::new) .collect(Collectors.toList(); assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public static final <A> <any> toBufferingDuplicator(java.util.Iterator<A> iterator)
public static final <A> <any> toBufferingDuplicator(java.util.Iterator<A> iterator,
long pos)
public static final <A> <any> toBufferingCopier(java.util.Iterator<A> iterator,
int copies)
public static final <A> java.util.Collection<A> toLazyCollection(<any> stream)
public static final <A> java.util.Collection<A> toConcurrentLazyCollection(<any> stream)
stream - public static final <T> <any> windowByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t)
public static final <T> <any> batchByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t)
public static final <T,C extends java.util.Collection<T>> <any> batchByTime(<any> stream,
long time,
java.util.concurrent.TimeUnit t,
java.util.function.Supplier<C> factory)
public static final <T> <any> windowStatefullyWhile(<any> stream,
java.util.function.BiPredicate<com.aol.cyclops.sequence.streamable.Streamable<T>,T> predicate)
public static final <T> <any> windowWhile(<any> stream,
java.util.function.Predicate<T> predicate)
public static final <T> <any> batchWhile(<any> stream,
java.util.function.Predicate<T> predicate)
public static final <T,C extends java.util.Collection<T>> <any> batchWhile(<any> stream,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<C> factory)
public static final <T> <any> batchUntil(<any> stream,
java.util.function.Predicate<T> predicate)
public static final <T> <any> batchBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t)
public static final <T,C extends java.util.Collection<T>> <any> batchBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t,
java.util.function.Supplier<C> factory)
public static final <T> <any> windowBySizeAndTime(<any> stream,
int size,
long time,
java.util.concurrent.TimeUnit t)
public static final <T> <any> debounce(<any> stream,
long time,
java.util.concurrent.TimeUnit t)
public static final <T> <any> onePer(<any> stream,
long time,
java.util.concurrent.TimeUnit t)
public static final <T> <any> jitter(<any> stream,
long jitterInNanos)
public static final <T> <any> fixedDelay(<any> stream,
long time,
java.util.concurrent.TimeUnit unit)
public static final <T> <any> xPer(<any> stream,
int x,
long time,
java.util.concurrent.TimeUnit t)
public static final <T> JavaslangHotStream<T> hotStream(<any> stream, java.util.concurrent.Executor exec)