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() |
Modifier and Type | Method and Description |
---|---|
static <A> AccumulateAnyArray<A> |
accumulateAnyArray(java.lang.Object underlying) |
static <A> AccumulatesFromStepper<A,Accumulator<A>> |
accumulateAnyStepper() |
static AccumulateDoubleArray |
accumulateDoubleArray(double[] underlying) |
static AccumulateDoubleCollection |
accumulateDoubleCollection(scala.collection.TraversableOnce<java.lang.Object> underlying) |
static java.lang.Object |
accumulateDoubleStepper() |
static AccumulateIntArray |
accumulateIntArray(int[] underlying) |
static AccumulateIntCollection |
accumulateIntCollection(scala.collection.TraversableOnce<java.lang.Object> underlying) |
static java.lang.Object |
accumulateIntStepper() |
static AccumulateLongArray |
accumulateLongArray(long[] underlying) |
static AccumulateLongCollection |
accumulateLongCollection(scala.collection.TraversableOnce<java.lang.Object> underlying) |
static java.lang.Object |
accumulateLongStepper() |
static <A> CollectionCanAccumulate<A> |
collectionCanAccumulate(scala.collection.TraversableOnce<A> underlying) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichAnySteppableWithParKeyStream(CC cc,
scala.Function1<CC,MakesKeyValueStepper<A,?,EfficientSubstep>> steppize,
StreamShape<A,S> ss) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichAnySteppableWithParStream(CC cc,
scala.Function1<CC,MakesStepper<A,EfficientSubstep>> steppize,
StreamShape<A,S> ss) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichAnySteppableWithParValueStream(CC cc,
scala.Function1<CC,MakesKeyValueStepper<?,A,EfficientSubstep>> steppize,
StreamShape<A,S> ss) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichAnySteppableWithSeqStream(CC cc,
scala.Function1<CC,MakesStepper<A,java.lang.Object>> steppize,
StreamShape<A,S> ss) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichScalaCollectionWithSeqKeyStream(CC cc,
scala.Function1<CC,MakesKeyValueStepper<A,?,java.lang.Object>> steppize,
StreamShape<A,S> ss) |
static <A,S extends java.util.stream.BaseStream<?,S>,CC> |
EnrichScalaCollectionWithSeqValueStream(CC cc,
scala.Function1<CC,MakesKeyValueStepper<?,A,java.lang.Object>> steppize,
StreamShape<A,S> ss) |
static java.lang.Object |
primitiveAccumulateDoubleStream() |
static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Double>,DoubleAccumulator> |
primitiveAccumulateDoubleStream2() |
static java.lang.Object |
primitiveAccumulateIntStream() |
static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Integer>,IntAccumulator> |
primitiveAccumulateIntStream2() |
static java.lang.Object |
primitiveAccumulateLongStream() |
static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Long>,LongAccumulator> |
primitiveAccumulateLongStream2() |
static java.lang.Object |
primitiveUnboxDoubleStream() |
static PrimitiveStreamUnboxer<java.lang.Double,java.util.stream.DoubleStream> |
primitiveUnboxDoubleStream2() |
static java.lang.Object |
primitiveUnboxIntStream() |
static PrimitiveStreamUnboxer<java.lang.Integer,java.util.stream.IntStream> |
primitiveUnboxIntStream2() |
static java.lang.Object |
primitiveUnboxLongStream() |
static PrimitiveStreamUnboxer<java.lang.Long,java.util.stream.LongStream> |
primitiveUnboxLongStream2() |
static <A> RichArrayCanStep<A> |
richArrayCanStep(java.lang.Object underlying) |
static RichBitSetCanStep |
richBitSetCanStep(scala.collection.BitSet underlying) |
static <K,V> RichDefaultHashTableCanStep<K,V> |
richDefaultHashTableCanStep(scala.collection.mutable.HashTable<K,scala.collection.mutable.DefaultEntry<K,V>> underlying) |
static <A> RichFlatHashTableCanStep<A> |
richFlatHashTableCanStep(scala.collection.mutable.FlatHashTable<A> underlying) |
static <K,V> RichImmHashMapCanStep<K,V> |
richImmHashMapCanStep(scala.collection.immutable.HashMap<K,V> underlying) |
static <A> RichImmHashSetCanStep<A> |
richImmHashSetCanStep(scala.collection.immutable.HashSet<A> underlying) |
static <A> RichIndexedSeqCanStep<A> |
richIndexedSeqCanStep(scala.collection.IndexedSeqLike<A,?> underlying) |
static <A> RichIterableCanStep<A> |
richIterableCanStep(scala.collection.Iterable<A> underlying) |
static <A> RichIteratorCanStep<A> |
richIteratorCanStep(scala.collection.Iterator<A> underlying) |
static <A> RichLinearSeqCanStep<A> |
richLinearSeqCanStep(scala.collection.LinearSeq<A> underlying) |
static <K,V> RichLinkedHashTableCanStep<K,V> |
richLinkedHashTableCanStep(scala.collection.mutable.HashTable<K,scala.collection.mutable.LinkedEntry<K,V>> underlying) |
static <K,V> RichMapCanStep<K,V> |
richMapCanStep(scala.collection.Map<K,V> underlying) |
static <T> RichNumericRangeCanStep<T> |
richNumericRangeCanStep(scala.collection.immutable.NumericRange<T> underlying) |
static RichRangeCanStep<scala.runtime.Nothing$> |
richRangeCanStep(scala.collection.immutable.Range underlying) |
static <A> Priority1StreamConverters.RichStream<A> |
RichStream(java.util.stream.Stream<A> stream) |
static <S> Priority1StreamConverters.RichStreamCanAccumulatePrimitive<S> |
RichStreamCanAccumulatePrimitive(S stream) |
static RichStringCanStep |
richStringCanStep(java.lang.String underlying) |
static <A> RichVectorCanStep<A> |
richVectorCanStep(scala.collection.immutable.Vector<A> underlying) |
static <A> RichArrayCanStep<A> |
richWrappedArrayCanStep(scala.collection.mutable.WrappedArray<A> underlying) |
public static java.lang.Object primitiveAccumulateDoubleStream()
public static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Double>,DoubleAccumulator> primitiveAccumulateDoubleStream2()
public static java.lang.Object primitiveUnboxDoubleStream()
public static PrimitiveStreamUnboxer<java.lang.Double,java.util.stream.DoubleStream> primitiveUnboxDoubleStream2()
public static java.lang.Object primitiveAccumulateIntStream()
public static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Integer>,IntAccumulator> primitiveAccumulateIntStream2()
public static java.lang.Object primitiveUnboxIntStream()
public static PrimitiveStreamUnboxer<java.lang.Integer,java.util.stream.IntStream> primitiveUnboxIntStream2()
public static java.lang.Object primitiveAccumulateLongStream()
public static PrimitiveStreamAccumulator<java.util.stream.Stream<java.lang.Long>,LongAccumulator> primitiveAccumulateLongStream2()
public static java.lang.Object primitiveUnboxLongStream()
public static PrimitiveStreamUnboxer<java.lang.Long,java.util.stream.LongStream> primitiveUnboxLongStream2()
public static java.lang.Object accumulateDoubleStepper()
public static java.lang.Object accumulateIntStepper()
public static java.lang.Object accumulateLongStepper()
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichAnySteppableWithParStream<A,S,CC> EnrichAnySteppableWithParStream(CC cc, scala.Function1<CC,MakesStepper<A,EfficientSubstep>> steppize, StreamShape<A,S> ss)
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichAnySteppableWithSeqStream<A,S,CC> EnrichAnySteppableWithSeqStream(CC cc, scala.Function1<CC,MakesStepper<A,java.lang.Object>> steppize, StreamShape<A,S> ss)
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichAnySteppableWithParKeyStream<A,S,CC> EnrichAnySteppableWithParKeyStream(CC cc, scala.Function1<CC,MakesKeyValueStepper<A,?,EfficientSubstep>> steppize, StreamShape<A,S> ss)
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichScalaCollectionWithSeqKeyStream<A,S,CC> EnrichScalaCollectionWithSeqKeyStream(CC cc, scala.Function1<CC,MakesKeyValueStepper<A,?,java.lang.Object>> steppize, StreamShape<A,S> ss)
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichAnySteppableWithParValueStream<A,S,CC> EnrichAnySteppableWithParValueStream(CC cc, scala.Function1<CC,MakesKeyValueStepper<?,A,EfficientSubstep>> steppize, StreamShape<A,S> ss)
public static <A,S extends java.util.stream.BaseStream<?,S>,CC> Priority2StreamConverters.EnrichScalaCollectionWithSeqValueStream<A,S,CC> EnrichScalaCollectionWithSeqValueStream(CC cc, scala.Function1<CC,MakesKeyValueStepper<?,A,java.lang.Object>> steppize, StreamShape<A,S> ss)
public static <A> Priority1StreamConverters.RichStream<A> RichStream(java.util.stream.Stream<A> stream)
public static <S> Priority1StreamConverters.RichStreamCanAccumulatePrimitive<S> RichStreamCanAccumulatePrimitive(S stream)
public static <A> RichIterableCanStep<A> richIterableCanStep(scala.collection.Iterable<A> underlying)
public static <K,V> RichMapCanStep<K,V> richMapCanStep(scala.collection.Map<K,V> underlying)
public static <A> RichLinearSeqCanStep<A> richLinearSeqCanStep(scala.collection.LinearSeq<A> underlying)
public static <A> RichIndexedSeqCanStep<A> richIndexedSeqCanStep(scala.collection.IndexedSeqLike<A,?> underlying)
public static <K,V> RichDefaultHashTableCanStep<K,V> richDefaultHashTableCanStep(scala.collection.mutable.HashTable<K,scala.collection.mutable.DefaultEntry<K,V>> underlying)
public static <K,V> RichLinkedHashTableCanStep<K,V> richLinkedHashTableCanStep(scala.collection.mutable.HashTable<K,scala.collection.mutable.LinkedEntry<K,V>> underlying)
public static <A> RichArrayCanStep<A> richArrayCanStep(java.lang.Object underlying)
public static <A> RichArrayCanStep<A> richWrappedArrayCanStep(scala.collection.mutable.WrappedArray<A> underlying)
public static <A> RichFlatHashTableCanStep<A> richFlatHashTableCanStep(scala.collection.mutable.FlatHashTable<A> underlying)
public static <A> RichIteratorCanStep<A> richIteratorCanStep(scala.collection.Iterator<A> underlying)
public static <K,V> RichImmHashMapCanStep<K,V> richImmHashMapCanStep(scala.collection.immutable.HashMap<K,V> underlying)
public static <A> RichImmHashSetCanStep<A> richImmHashSetCanStep(scala.collection.immutable.HashSet<A> underlying)
public static <T> RichNumericRangeCanStep<T> richNumericRangeCanStep(scala.collection.immutable.NumericRange<T> underlying)
public static <A> RichVectorCanStep<A> richVectorCanStep(scala.collection.immutable.Vector<A> underlying)
public static RichBitSetCanStep richBitSetCanStep(scala.collection.BitSet underlying)
public static RichRangeCanStep<scala.runtime.Nothing$> richRangeCanStep(scala.collection.immutable.Range underlying)
public static RichStringCanStep richStringCanStep(java.lang.String underlying)
public static <A> CollectionCanAccumulate<A> collectionCanAccumulate(scala.collection.TraversableOnce<A> underlying)
public static AccumulateDoubleCollection accumulateDoubleCollection(scala.collection.TraversableOnce<java.lang.Object> underlying)
public static AccumulateIntCollection accumulateIntCollection(scala.collection.TraversableOnce<java.lang.Object> underlying)
public static AccumulateLongCollection accumulateLongCollection(scala.collection.TraversableOnce<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)
public static <A> AccumulatesFromStepper<A,Accumulator<A>> accumulateAnyStepper()