A Java 8 compatibility kit for Scala.
The API is currently still experimental: we do not yet guarantee source or binary compatibility with future releases.
A set of Functional Interfaces
for scala.FunctionN. These are designed for convenient construction of Scala functions
using Java 8 lambda syntax.
import scala.concurrent.*;
import static scala.compat.java8.JFunction.*;
class Test {
private static Future<Integer> futureExample(Future<String> future, ExecutionContext ec) {
return future.map(func(s -> s.toUpperCase()), ec).map(func(s -> s.length()), ec);
}
}A set of converters that enable interconversion between Java's standard
Functional Interfaces defined in java.util.function and Scala's Function0,
Function1, and Function2 traits. These are intended for use when you
already have an instance of a java.util.function and need a Scala function,
or have a Scala function and need an instance of a java.util.function.
The .asScala extension method will convert a java.util.function to the corresponding
Scala function. The .asJava extension method will convert a Scala function to
the most specific corresponding Java functional interface. If you wish to obtain
a less specific functional interface, there are named methods that start with asJava
and continue with the name of the Java functional interface. For instance, the
most specific interface corresponding to the Scala function val rev = (s: String) => s.reverse
is UnaryOperator[String], and that is what rev.asJava will produce. However,
asJavaFunction(rev) will return a java.util.function.Function[String, String] instead.
The asJava methods can also be called conveniently from Java. There are additional
asScalaFrom methods (e.g. asScalaFromUnaryOperator) that will perform the
functional-interface-to-Scala-function conversion; this is primarily of use when calling
from Java since the .asScala extension method is more convenient in Scala.
In Scala:
import java.util.function._
import scala.compat.java8.FunctionConverters._
val foo: Int => Boolean = i => i > 7
def testBig(ip: IntPredicate) = ip.test(9)
println(testBig(foo.asJava)) // Prints true
val bar = new UnaryOperator[String]{ def apply(s: String) = s.reverse }
List("cod", "herring").map(bar.asScala) // List("doc", "gnirrih")
def testA[A](p: Predicate[A])(a: A) = p.test(a)
println(testA(asJavaPredicate(foo))(4)) // Prints false
// println(testA(foo.asJava)(4)) <-- doesn't work
// IntPredicate does not extend Predicate!In Java:
import java.util.function.*;
import scala.compat.java8.FunctionConverters;
class Example {
String foo(UnaryOperator<String> f) {
return f.apply("halibut");
}
String bar(scala.Function1<String, String> f) {
return foo(functionConverters.asJavaUnaryOperator(f));
}
String baz(Function<String, String> f) {
return bar(functionConverters.asScalaFromFunction(f));
}
}Converters between scala.Option and java.util classes Optional, OptionalDouble, OptionalInt, and OptionalLong.
A set of extension methods to enable explicit conversion between Scala Option and the Java 8 optional types, Optional, OptionalDouble, OptionalInt, and OptionalLong.
Note that the four Java classes have no inheritance relationship despite all encoding optional types.
import scala.compat.java8.OptionConverters._
class Test {
val o = Option(2.7)
val oj = o.asJava // Optional[Double]
val ojd = o.asPrimitive // OptionalDouble
val ojds = ojd.asScala // Option(2.7) again
}- Converters for
java.util.stream Spliterators for Scala collections