public class StreamConverters
extends java.lang.Object
StreamConverters
provides extension methods and other functionality to
ease interoperability of Scala collections with java.util.stream
classes.
Scala collections gain extension methods seqStream
and
parStream
that allow them to be used as the source of a Stream
.
Some collections either intrinsically cannot be paralellized, or
could be but an efficient implementation is missing. It this case,
only seqStream
is provided. If a collection cannot be stepped over
at all (e.g. Traversable
), then it gains neither method.
Array
also gains seqStream
and parStream
methods, and calling those
on Array[Double]
, Array[Int]
, or Array[Long]
will produce the
corresponding primitive stream.
Streams gain accumulate
and toScala[_]
methods, which collect the stream
into a custom high-performance scala.collection.mutable.java8.Accumulator
,
which is not part of the standard collections hierarchy, or into a named
Scala collection, respectively.
Generic streams also gain an unboxed
method that will convert to the
corresponding unboxed primitive stream, if appropriate. Unboxed streams
have custom accumulators with improved performance.
Accumulators have toArray
, toList
, iterator
, and to[_]
methods
to convert to standard Scala collections. Note that if you wish to
create an array from a Stream
, going through an Accumulator
is
not the most efficient option: just create the Array
directly.
Internally, Scala collections implement a hybrid of Iterator
and
java.util.Spliterator
to implement Stream
compatibility; these
are called Stepper
s. In particular, they can test for the presence
of a next element using hasStep
, can retrieve the next value with
nextStep
, or can optionally retrieve and operate on a value if present
with tryStep
, which works like tryAdvance
in java.util.Spliterator
.
Every Scala collection that can be stepped
through has a stepper
method implicitly provided. In addition,
maps have keyStepper
and valueStepper
methods. A limited number
of collections operations are defined on Stepper
s, including conversion
to Scala collections with to
or accumulation via accumulate
.
Stepper
s also implement seqStream
and parStream
to generate Stream
s.
These are provided regardless of whether a Stepper
can efficiently
subdivide itself for parallel processing (though one can check for the
presence of the EfficientSubstep
trait to know that parallel execution will
not be limited by long sequential searching steps, and one can call
anticipateParallelism
to warn a Stepper
that it will be used in a parallel
context and thus may wish to make different tradeoffs).
Examples:
import scala.compat.java8.StreamConverters._
val s = Vector(1,2,3,4).parStream // Stream[Int]
val si = s.unboxed // Stream.OfInt
val ai = si.accumulate // IntAccumulator
val v = ai.to[Vector] // Vector[Int] again
val t = Array(2.0, 3.0, 4.0).parStream // DoubleStream
val q = t.toScala[scala.collection.immutable.Queue] // Queue[Double]
val x = List(1L, 2L, 3L, 4L).stepper.parStream.sum // 10, potentially computed in parallel
Constructor and Description |
---|
StreamConverters() |
public static StepperExtensions<java.lang.Object> richIntStepper(scala.collection.Stepper<java.lang.Object> s)
public static StepperExtensions<java.lang.Object> richLongStepper(scala.collection.Stepper<java.lang.Object> s)
public static StepperExtensions<java.lang.Object> richDoubleStepper(scala.collection.Stepper<java.lang.Object> s)
public static <A> StepperExtensions<A> richStepper(scala.collection.Stepper<A> s)
public static <A> StreamExtensions.IterableHasSeqStream<A> IterableHasSeqStream(scala.collection.IterableOnce<A> cc)
public static <A,C extends scala.collection.IterableOnce<?>> StreamExtensions.IterableNonGenericHasParStream<A,C> IterableNonGenericHasParStream(C c, scala.$less$colon$less<C,scala.collection.IterableOnce<A>> ev)
public static <K,V,CC extends scala.collection.MapOps<java.lang.Object,java.lang.Object,CC,?>> StreamExtensions.MapHasSeqKeyValueStream<K,V,CC> MapHasSeqKeyValueStream(CC cc)
public static <K,V,CC extends scala.collection.MapOps<java.lang.Object,java.lang.Object,CC,?>> StreamExtensions.MapHasParKeyValueStream<K,V,CC> MapHasParKeyValueStream(CC cc)
public static <A> StreamExtensions.StepperHasSeqStream<A> StepperHasSeqStream(scala.collection.Stepper<A> stepper)
public static <A> StreamExtensions.StepperHasParStream<A> StepperHasParStream(scala.collection.Stepper<A> stepper)
public static StreamExtensions.DoubleArrayHasSeqParStream DoubleArrayHasSeqParStream(double[] a)
public static StreamExtensions.IntArrayHasSeqParStream IntArrayHasSeqParStream(int[] a)
public static StreamExtensions.LongArrayHasSeqParStream LongArrayHasSeqParStream(long[] a)
public static <A> StreamExtensions.AnyArrayHasSeqParStream<A> AnyArrayHasSeqParStream(A[] a)
public static StreamExtensions.ByteArrayHasSeqParStream ByteArrayHasSeqParStream(byte[] a)
public static StreamExtensions.ShortArrayHasSeqParStream ShortArrayHasSeqParStream(short[] a)
public static StreamExtensions.CharArrayHasSeqParStream CharArrayHasSeqParStream(char[] a)
public static StreamExtensions.FloatArrayHasSeqParStream FloatArrayHasSeqParStream(float[] a)
public static <A> StreamExtensions.StreamHasToScala<A> StreamHasToScala(java.util.stream.Stream<A> stream)
public static StreamExtensions.StreamIntHasAccumulatePrimitive StreamIntHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Object> s)
public static StreamExtensions.StreamLongHasAccumulatePrimitive StreamLongHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Object> s)
public static StreamExtensions.StreamDoubleHasAccumulatePrimitive StreamDoubleHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Object> s)
public static StreamExtensions.StreamJIntegerHasAccumulatePrimitive StreamJIntegerHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Integer> s)
public static StreamExtensions.StreamJLongHasAccumulatePrimitive StreamJLongHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Long> s)
public static StreamExtensions.StreamJDoubleHasAccumulatePrimitive StreamJDoubleHasAccumulatePrimitive(java.util.stream.Stream<java.lang.Double> s)
public static StreamExtensions.IntStreamHasToScala IntStreamHasToScala(java.util.stream.IntStream stream)
public static StreamExtensions.LongStreamHasToScala LongStreamHasToScala(java.util.stream.LongStream stream)
public static StreamExtensions.DoubleStreamHasToScala DoubleStreamHasToScala(java.util.stream.DoubleStream stream)
public static <A> CollectionCanAccumulate<A> collectionCanAccumulate(scala.collection.IterableOnce<A> underlying)
public static AccumulateDoubleCollection accumulateDoubleCollection(scala.collection.IterableOnce<java.lang.Object> underlying)
public static AccumulateIntCollection accumulateIntCollection(scala.collection.IterableOnce<java.lang.Object> underlying)
public static AccumulateLongCollection accumulateLongCollection(scala.collection.IterableOnce<java.lang.Object> underlying)
public static <A> AccumulateAnyArray<A> accumulateAnyArray(java.lang.Object underlying)
public static AccumulateDoubleArray accumulateDoubleArray(double[] underlying)
public static AccumulateIntArray accumulateIntArray(int[] underlying)
public static AccumulateLongArray accumulateLongArray(long[] underlying)