Using Scala from Java: passing functions as parameters

In the scala.runtime package, there are abstract classes named AbstractFunction1 and so on for other arities. To use them from Java you only need to override apply, like this: Function1<Integer, String> f = new AbstractFunction1<Integer, String>() { public String apply(Integer someInt) { return myFunc(someInt); } }; If you’re on Java 8 and want to use … Read more

Convert Java Map to Scala Map

Edit: the recommended way is now to use JavaConverters and the .asScala method: import scala.collection.JavaConverters._ val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet) This has the advantage of not using magical implicit conversions but explicit calls to .asScala, while staying clean and consise. The original answer with JavaConversions: You can use scala.collection.JavaConversions to implicitly convert between Java and Scala: … Read more

How can I convert a Java Iterable to a Scala Iterable?

In Scala 2.8 this became much much easier, and there are two ways to achieve it. One that’s sort of explicit (although it uses implicits): import scala.collection.JavaConverters._ val myJavaIterable = someExpr() val myScalaIterable = myJavaIterable.asScala EDIT: Since I wrote this, the Scala community has arrived at a broad consensus that JavaConverters is good, and JavaConversions … Read more

Converting Java to Scala durations

I don’t know whether an explicit conversion is the only way, but if you want to do it right FiniteDuration(d.toNanos, TimeUnit.NANOSECONDS) toNanos will return the total duration, while getNano will only return the nanoseconds component, which is not what you want. E.g. import java.time.Duration import jata.time.temporal.ChronoUnit Duration.of(1, ChronoUnit.HOURS).getNano // 0 Duration.of(1, ChronoUnit.HOURS).toNanos // 3600000000000L That … Read more

Using Scala traits with implemented methods in Java

Answer From Java perspective Trait.scala is compiled into Trait interface. Hence implementing Trait in Java is interpreted as implementing an interface – which makes your error messages obvious. Short answer: you can’t take advantage of trait implementations in Java, because this would enable multiple inheritance in Java (!) How is it implemented in Scala? Long … Read more

How to convert a scala.List to a java.util.List?

Not sure why this hasn’t been mentioned before but I think the most intuitive way is to invoke the asJava decorator method of JavaConverters directly on the Scala list: scala> val scalaList = List(1,2,3) scalaList: List[Int] = List(1, 2, 3) scala> import scala.collection.JavaConverters._ import scala.collection.JavaConverters._ scala> scalaList.asJava res11: java.util.List[Int] = [1, 2, 3]

What is the difference between JavaConverters and JavaConversions in Scala?

EDIT: Java Conversions got @deprecated in Scala 2.13.0. Use scala.jdk.CollectionConverters instead. JavaConversions provide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating wrappers that implement either the Scala interface and forward the calls to the underlying Java collection, or … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)