Package org.codehaus.groovy.runtime
Class StreamGroovyMethods
java.lang.Object
org.codehaus.groovy.runtime.StreamGroovyMethods
-
Method Summary
Modifier and TypeMethodDescriptionstatic DoubleStream
doubleStream
(double[] self) Returns a sequentialDoubleStream
with the specified array as its source.static <T> Stream<T>
from
(Stream<T> self, EmptyRange range) Returns an empty stream.static <T> Stream<T>
Returns a (possibly empty) stream.static <T> T
Returns element atindex
ornull
.static <T> List<T>
getAt
(Stream<T> self, EmptyRange range) Returns an empty list.static <T> List<T>
Returns element(s) inrange
or an empty list.static IntStream
intStream
(int[] self) Returns a sequentialIntStream
with the specified array as its source.static LongStream
longStream
(long[] self) Returns a sequentialLongStream
with the specified array as its source.static <T> Stream<T>
Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of theIterable
object.static <T> Stream<T>
plus
(Stream<? extends T> lhs, Collection<? extends T> rhs) Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of theCollection
object.static <T> Stream<T>
Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the second stream.stream
(boolean[] self) Returns a sequentialStream
with the specified array as its source.stream
(byte[] self) Returns a sequentialStream
with the specified array as its source.stream
(char[] self) Returns a sequentialStream
with the specified array as its source.stream
(double[] self) Returns a sequentialStream
with the specified array as its source.stream
(float[] self) Returns a sequentialStream
with the specified array as its source.stream
(int[] self) Returns a sequentialStream
with the specified array as its source.stream
(long[] self) Returns a sequentialStream
with the specified array as its source.stream
(short[] self) Returns a sequentialStream
with the specified array as its source.static <T> Stream<T>
Returns a sequentialStream
with the specified element(s) as its source.static <T> Stream<T>
stream
(Enumeration<T> self) Returns a sequentialStream
with the specified element(s) as its source.static <T> Stream<T>
Returns a sequentialStream
with the specified element(s) as its source.static <T> Stream<T>
static DoubleStream
stream
(OptionalDouble self) If a value is present in theOptionalDouble
, returns aDoubleStream
with the value as its source or else an empty stream.static IntStream
stream
(OptionalInt self) If a value is present in theOptionalInt
, returns anIntStream
with the value as its source or else an empty stream.static LongStream
stream
(OptionalLong self) If a value is present in theOptionalLong
, returns aLongStream
with the value as its source or else an empty stream.static <T> Stream<T>
stream
(Spliterator<T> self) Returns a sequentialStream
with the specified element(s) as its source.static <T> Stream<T>
stream
(NullObject self) Returns an empty sequentialStream
.static <T> Stream<T>
stream
(T self) Returns a sequentialStream
containing a single element.static <T> Stream<T>
stream
(T[] self) Returns a sequentialStream
with the specified array as its source.static <T> T[]
Returns an array containing the elements of the stream.static <T> List<T>
toList
(BaseStream<T, ? extends BaseStream> self) Accumulates the elements of stream into a new List.static <T> List<T>
Accumulates the elements of stream into a new List.static <T> Set<T>
toSet
(BaseStream<T, ? extends BaseStream> self) Accumulates the elements of stream into a new Set.static <T> Set<T>
Accumulates the elements of stream into a new Set.
-
Method Details
-
getAt
Returns element atindex
ornull
.This is a short-circuiting terminal operation.
import java.util.stream.Stream import static groovy.test.GroovyAssert.shouldFail Stream
stream = ['foo','bar','baz'].stream() shouldFail(IllegalArgumentException) { stream[-1] } stream = ['foo','bar','baz'].stream() assert stream[0] == 'foo' stream = ['foo','bar','baz'].stream() assert stream[1] == 'bar' stream = ['foo','bar','baz'].stream() assert stream[2] == 'baz' stream = ['foo','bar','baz'].stream() assert stream[3] === null - Throws:
IllegalArgumentException
- ifindex
is negative- Since:
- 5.0.0
-
getAt
Returns element(s) inrange
or an empty list.This is a short-circuiting terminal operation.
import java.util.stream.Stream import static groovy.test.GroovyAssert.shouldFail Stream<String> stream = ['foo','bar','baz'].stream() shouldFail(IllegalArgumentException) { stream[-1..0] } stream = ['foo','bar','baz'].stream() shouldFail(IllegalArgumentException) { stream[0..-1] } stream = ['foo','bar','baz'].stream() assert stream[0..<1] == ['foo'] stream = ['foo','bar','baz'].stream() assert stream[1..<2] == ['bar'] stream = ['foo','bar','baz'].stream() assert stream[2..<3] == ['baz'] stream = ['foo','bar','baz'].stream() assert stream[3..<4] == [] stream = ['foo','bar','baz'].stream() assert stream[0<..2] == ['bar','baz'] stream = ['foo','bar','baz'].stream() assert stream[0..99] == ['foo','bar','baz']
- Throws:
IllegalArgumentException
- for negative index or reverse range- Since:
- 5.0.0
-
getAt
Returns an empty list.import java.util.stream.Stream Stream<String> stream = ['foo','bar','baz'].stream() assert stream[1..<1].isEmpty()
- Since:
- 5.0.0
-
from
Returns a (possibly empty) stream.This is a short-circuiting intermediate operation.
import java.util.stream.Stream import static groovy.test.GroovyAssert.shouldFail Stream<String> stream = ['foo','bar','baz'].stream() shouldFail(IllegalArgumentException) { stream.from(-1..0) } stream = ['foo','bar','baz'].stream() shouldFail(IllegalArgumentException) { stream.from(0..-1) } stream = ['foo','bar','baz'].stream() assert stream.from(0..<1).toList() == ['foo'] stream = ['foo','bar','baz'].stream() assert stream.from(1..<2).toList() == ['bar'] stream = ['foo','bar','baz'].stream() assert stream.from(2..<3).toList() == ['baz'] stream = ['foo','bar','baz'].stream() assert stream.from(3..<4).toList() == [] stream = ['foo','bar','baz'].stream() assert stream.from(0<..2).toList() == ['bar','baz'] stream = ['foo','bar','baz'].stream() assert stream.from(0<..<2).toList() == ['bar'] stream = ['foo','bar','baz'].stream() assert stream.from(0..99).toList() == ['foo','bar','baz']
- Throws:
IllegalArgumentException
- for negative index or reverse range- Since:
- 5.0.0
-
from
Returns an empty stream.import java.util.stream.Stream Stream<String> stream = ['foo','bar','baz'].stream() assert !stream.from(1..<1).findAny().isPresent()
- Since:
- 5.0.0
-
plus
Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of theCollection
object.import java.util.stream.Stream assert (Stream.of(1) + [2]).toList() == [1,2] assert (Stream.of(1) + []).toList() == [1]
- Since:
- 4.0.0
-
plus
Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of theIterable
object.import java.util.stream.Stream assert (Stream.of(1) + [2]).toList() == [1,2] assert (Stream.of(1) + []).toList() == [1]
- Since:
- 4.0.0
-
plus
Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the second stream.import java.util.stream.Stream assert (Stream.of(1) + Stream.<Integer>empty()).toList() == [1] assert (Stream.of(1) + Stream.of(2)).toList() == [1,2] assert (Stream.of(1) + [2].stream()).toList() == [1,2]
- Since:
- 4.0.0
-
stream
Returns a sequentialStream
containing a single element.def item = 'string' assert item.stream().toList() == ['string'] assert item.stream().findFirst().isPresent()
- Since:
- 3.0.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Type Parameters:
T
- The type of the array elements- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 2.5.0
-
stream
Returns a sequentialStream
with the specified element(s) as its source.def tokens = new StringTokenizer('one two') assert tokens.stream().toList() == ['one', 'two']
- Since:
- 3.0.0
-
stream
Returns a sequentialStream
with the specified element(s) as its source.class Items implements Iterable
{ Iterator<String> iterator() { ['one', 'two'].iterator() } } def items = new Items() assert items.stream().toList() == ['one', 'two'] - Since:
- 3.0.0
-
stream
Returns a sequentialStream
with the specified element(s) as its source.[].iterator().stream().toList().isEmpty() ['one', 'two'].iterator().stream().toList() == ['one', 'two']
- Since:
- 3.0.0
-
stream
Returns a sequentialStream
with the specified element(s) as its source.assert [].spliterator().stream().toList().isEmpty() assert ['one', 'two'].spliterator().stream().toList() == ['one', 'two']
- Since:
- 3.0.0
-
stream
Returns an empty sequentialStream
.def item = null assert item.stream().toList() == [] assert !item.stream().findFirst().isPresent()
- Since:
- 3.0.0
-
stream
If a value is present in theOptional
, returns aStream
with the value as its source or else an empty stream.- Since:
- 3.0.0
-
stream
If a value is present in theOptionalInt
, returns anIntStream
with the value as its source or else an empty stream.- Since:
- 3.0.0
-
stream
If a value is present in theOptionalLong
, returns aLongStream
with the value as its source or else an empty stream.- Since:
- 3.0.0
-
stream
If a value is present in theOptionalDouble
, returns aDoubleStream
with the value as its source or else an empty stream.- Since:
- 3.0.0
-
intStream
Returns a sequentialIntStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 3.0.8
-
longStream
Returns a sequentialLongStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 3.0.8
-
doubleStream
Returns a sequentialDoubleStream
with the specified array as its source.- Parameters:
self
- The array, assumed to be unmodified during use- Returns:
- a
Stream
for the array - Since:
- 3.0.8
-
toArray
Returns an array containing the elements of the stream.import static groovy.test.GroovyAssert.shouldFail assert Arrays.equals([].stream().toArray(Object), new Object[0]) assert Arrays.equals([].stream().toArray(String), new String[0]) assert Arrays.equals([].stream().toArray(String[]), new String[0][]) assert Arrays.equals(['x'].stream().toArray(Object), ['x'].toArray()) assert Arrays.equals(['x'].stream().toArray(String), ['x'] as String[]) assert Arrays.deepEquals([['x'] as String[]].stream().toArray(String[]), [['x'] as String[]] as String[][]) assert Arrays.equals(['x'].stream().toArray(CharSequence), ['x'] as CharSequence[]) shouldFail(ArrayStoreException) { ['x'].stream().toArray(Thread) } shouldFail(IllegalArgumentException) { ['x'].stream().toArray((Class) null) } // Stream#toArray(IntFunction) should still be used for closure literal: assert Arrays.equals(['x'].stream().toArray { n -> new String[n] }, ['x'] as String[]) // Stream#toArray(IntFunction) should still be used for method reference: assert Arrays.equals(['x'].stream().toArray(String[]::new), ['x'] as String[])
- Parameters:
self
- the streamtype
- the array element type- Since:
- 3.0.4
-
toList
Accumulates the elements of stream into a new List.- Type Parameters:
T
- the type of element- Parameters:
self
- the stream- Returns:
- a new
java.util.List
instance - Since:
- 2.5.0
-
toList
Accumulates the elements of stream into a new List.- Type Parameters:
T
- the type of element- Parameters:
self
- thejava.util.stream.BaseStream
- Returns:
- a new
java.util.List
instance - Since:
- 2.5.0
-
toSet
Accumulates the elements of stream into a new Set.- Type Parameters:
T
- the type of element- Parameters:
self
- the stream- Returns:
- a new
java.util.Set
instance - Since:
- 2.5.0
-
toSet
Accumulates the elements of stream into a new Set.- Type Parameters:
T
- the type of element- Parameters:
self
- thejava.util.stream.BaseStream
- Returns:
- a new
java.util.Set
instance - Since:
- 2.5.0
-