package java8
- Alphabetic
- Public
- Protected
Package Members
- package converterImpl
- package wrappers
Type Members
- class ScalaStreamSupport extends AnyRef
This class contains static utility methods for creating Java Streams from Scala Collections, similar to the methods in
java.util.stream.StreamSupport
for other Java types.This class contains static utility methods for creating Java Streams from Scala Collections, similar to the methods in
java.util.stream.StreamSupport
for other Java types. It is intended for use from Java code. In Scala code, you can use the extension methods provided byscala.compat.java8.StreamConverters
instead.Streams created from immutable Scala collections are also immutable. Mutable collections should not be modified concurrently. There are no guarantees for success or failure modes of existing streams in case of concurrent modifications.
- trait StreamExtensions extends AnyRef
Defines extension methods to create Java Streams for Scala collections, available through scala.compat.java8.StreamConverters.
- trait WrappedAsJava[J] extends AnyRef
A trait that indicates that the class is or can be converted to a Java version by wrapping a Scala class
- trait WrappedAsScala[S] extends AnyRef
A trait that indicates that the class is or can be converted to a Scala version by wrapping a Java class
Value Members
- object DurationConverters
This class contains static methods which convert between Java Durations and the durations from the Scala concurrency package.
This class contains static methods which convert between Java Durations and the durations from the Scala concurrency package. This is useful when mediating between Scala and Java libraries with asynchronous APIs where timeouts for example are often expressed as durations.
- object FutureConverters
This class contains static methods which convert between Java CompletionStage and Scala Future.
This class contains static methods which convert between Java CompletionStage and Scala Future. This is useful when mediating between Scala and Java libraries with asynchronous APIs.
Note that the bridge is implemented at the read-only side of asynchronous handles, namely scala.concurrent.Future instead of scala.concurrent.Promise and CompletionStage instead of CompletableFuture. This is intentional, as the semantics of bridging the write-handles would be prone to race conditions; if both ends (CompletableFuture and Promise) are completed independently at the same time, they may contain different values afterwards. For this reason,
toCompletableFuture()
is not supported on the created CompletionStages.Example usage:
import java.util.concurrent.CompletionStage; import scala.concurrent.Future; import static scala.concurrent.java8.FutureConverters.*; final CompletionStage<String> cs = ... // from an async Java API final Future<String> f = toScala(cs); ... final Future<Integer> f2 = ... // from an async Scala API final CompletionStage<Integer> cs2 = toJava(f2);
- object OptionConverters
This class enables bidirectional conversion between
scala.Option
and the set ofjava.util.Optional
classes.This class enables bidirectional conversion between
scala.Option
and the set ofjava.util.Optional
classes.The Scala
Option
is generic; its generic counterpart in Java isjava.util.Optional
.Option
is enriched with anasJava
method, whileOptional
is enriched withasScala
to perform conversions.In addition, both
Option
andOptional
are enriched withasPrimitive
methods that will convert generically contained primitives to the manually specialized Java versions for primitives,OptionalDouble
,OptionalInt
, andOptionalLong
. The primitive versions can be converted to the Scala genericOption
withasScala
and to the Java genericOptional
withasGeneric
.When calling from Java, methods are more convenient than extension methods, so
toJava
andtoScala
methods are provided that convert to and from Scala'sOption
. Note thattoJava(toScala(x))
will result in a genericOptional
even ifx
was one of the primitive versons.Example usage:
import scala.compat.java8.OptionConverters._ val a = Option("example").asJava // Creates java.util.Optional[String] containing "example" val b = (None: Option[String]).asJava // Creates an empty java.util.Optional[String] val c = a.asScala // Back to Option("example") val d = b.asScala // Back to None typed as Option[String] val e = Option(2.7).asJava // java.util.Optional[Double] containing boxed 2.7 val f = Option(2.7).asPrimitive // java.util.OptionalDouble containing 2.7 (not boxed) val g = f.asScala // Back to Option(2.7) val h = f.asGeneric // Same as e val i = e.asPrimitive // Same as f val j = toJava(Option("example")) // Same as a val k = toScala(a) // Same as c
- object PrimitiveIteratorConverters
This class enables conversion from
scala.Iterator
to the set ofjava.util.PrimitiveIterator
classes.This class enables conversion from
scala.Iterator
to the set ofjava.util.PrimitiveIterator
classes.Scala's
Iterator
is generic, as is itsjava.util
counterpart. However,java.util.PrimitiveIterator
offers three manually-specialized variants ofIterator
:OfDouble
,OfInt
, andOfLong
. This class provides.asPrimitive
extension methods for Scala and Java iterators to present the generic versions as the specialized version.Example usage:
import scala.compat.java8.PrimitiveIteratorConverters._ val it = Iterator(1.0, 2.0, math.Pi) val jpid = it.asPrimitive // PrimitiveIterator.OfDouble
- object StreamConverters extends StreamExtensions with Priority1AccumulatorConverters
StreamConverters
provides extension methods and other functionality to ease interoperability of Scala collections withjava.util.stream
classes.StreamConverters
provides extension methods and other functionality to ease interoperability of Scala collections withjava.util.stream
classes.Scala collections gain extension methods
seqStream
andparStream
that allow them to be used as the source of aStream
. Some collections either intrinsically cannot be paralellized, or could be but an efficient implementation is missing. It this case, onlyseqStream
is provided. If a collection cannot be stepped over at all (e.g.Traversable
), then it gains neither method.Array
also gainsseqStream
andparStream
methods, and calling those onArray[Double]
,Array[Int]
, orArray[Long]
will produce the corresponding primitive stream.Streams gain
accumulate
andtoScala[_]
methods, which collect the stream into a custom high-performancescala.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
, andto[_]
methods to convert to standard Scala collections. Note that if you wish to create an array from aStream
, going through anAccumulator
is not the most efficient option: just create theArray
directly.Internally, Scala collections implement a hybrid of
Iterator
andjava.util.Spliterator
to implementStream
compatibility; these are calledStepper
s. In particular, they can test for the presence of a next element usinghasStep
, can retrieve the next value withnextStep
, or can optionally retrieve and operate on a value if present withtryStep
, which works liketryAdvance
injava.util.Spliterator
.Every Scala collection that can be stepped through has a
stepper
method implicitly provided. In addition, maps havekeyStepper
andvalueStepper
methods. A limited number of collections operations are defined onStepper
s, including conversion to Scala collections withto
or accumulation viaaccumulate
.Stepper
s also implementseqStream
andparStream
to generateStream
s. These are provided regardless of whether aStepper
can efficiently subdivide itself for parallel processing (though one can check for the presence of theEfficientSubstep
trait to know that parallel execution will not be limited by long sequential searching steps, and one can callanticipateParallelism
to warn aStepper
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