Accessing scala.None from Java
This might work: final scala.Option<String> x = scala.Option.apply(null); def apply [A] (x: A): Option[A] An Option factory which creates Some(x) if the argument is not null, and None if it is null.
This might work: final scala.Option<String> x = scala.Option.apply(null); def apply [A] (x: A): Option[A] An Option factory which creates Some(x) if the argument is not null, and None if it is null.
You can simply convert the List using Scala’s JavaConverters: import scala.collection.JavaConverters._ def findAllQuestion():List[Question] = { questionDao.getAllQuestions().asScala.toList }
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
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]
selenium.`type`(“ab”,”abc”)
Use Person$.MODULE$. See also How can I pass a Scala object reference around in Java? Singletons as Synthetic classes in Scala? Edit: A working example (I checked, it compiles and works): Scala: object Person { val MALE = “m”; } Java counterpart: public class Test { Person$ myvar = Person$.MODULE$; public static void main(String argv[]) … Read more
You normally use ==, it routes to equals, except that it treats nulls properly. Reference equality (rarely used) is eq.
As of Scala 2.8, all you have to do is to import the JavaConversions object, which already declares the appropriate conversions. import scala.collection.JavaConversions._ This won’t work in previous versions though.
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
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