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.StreamSupportfor other Java types.This class contains static utility methods for creating Java Streams from Scala Collections, similar to the methods in
java.util.stream.StreamSupportfor other Java types. It is intended for use from Java code. In Scala code, you can use the extension methods provided byscala.compat.java8.StreamConvertersinstead.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.Optionand the set ofjava.util.Optionalclasses.This class enables bidirectional conversion between
scala.Optionand the set ofjava.util.Optionalclasses.The Scala
Optionis generic; its generic counterpart in Java isjava.util.Optional.Optionis enriched with anasJavamethod, whileOptionalis enriched withasScalato perform conversions.In addition, both
OptionandOptionalare enriched withasPrimitivemethods 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 genericOptionwithasScalaand to the Java genericOptionalwithasGeneric.When calling from Java, methods are more convenient than extension methods, so
toJavaandtoScalamethods are provided that convert to and from Scala'sOption. Note thattoJava(toScala(x))will result in a genericOptionaleven ifxwas 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.Iteratorto the set ofjava.util.PrimitiveIteratorclasses.This class enables conversion from
scala.Iteratorto the set ofjava.util.PrimitiveIteratorclasses.Scala's
Iteratoris generic, as is itsjava.utilcounterpart. However,java.util.PrimitiveIteratoroffers three manually-specialized variants ofIterator:OfDouble,OfInt, andOfLong. This class provides.asPrimitiveextension 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
StreamConvertersprovides extension methods and other functionality to ease interoperability of Scala collections withjava.util.streamclasses.StreamConvertersprovides extension methods and other functionality to ease interoperability of Scala collections withjava.util.streamclasses.Scala collections gain extension methods
seqStreamandparStreamthat 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, onlyseqStreamis provided. If a collection cannot be stepped over at all (e.g.Traversable), then it gains neither method.Arrayalso gainsseqStreamandparStreammethods, and calling those onArray[Double],Array[Int], orArray[Long]will produce the corresponding primitive stream.Streams gain
accumulateandtoScala[_]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
unboxedmethod 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 anAccumulatoris not the most efficient option: just create theArraydirectly.Internally, Scala collections implement a hybrid of
Iteratorandjava.util.Spliteratorto implementStreamcompatibility; these are calledSteppers. 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 liketryAdvanceinjava.util.Spliterator.Every Scala collection that can be stepped through has a
steppermethod implicitly provided. In addition, maps havekeyStepperandvalueSteppermethods. A limited number of collections operations are defined onSteppers, including conversion to Scala collections withtoor accumulation viaaccumulate.Steppers also implementseqStreamandparStreamto generateStreams. These are provided regardless of whether aSteppercan efficiently subdivide itself for parallel processing (though one can check for the presence of theEfficientSubsteptrait to know that parallel execution will not be limited by long sequential searching steps, and one can callanticipateParallelismto warn aStepperthat 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