score:25
Accepted answer
You can use:
scala.collection.Seq$.MODULE$.empty();
from Java code to create an empty sequence. Otherwise, you can use:
new scala.collection.mutable.ArrayBuffer();
to create an empty array buffer into which you can then add elements and use it as an argument to Scala vararg methods.
Otherwise, if you design a Scala library with vararg methods which you want to use from Java code, then use the varargs
annotation. It will generate a Java version of the method which takes an array instead of a Seq
.
scala> class A {
| @annotation.varargs def foo(x: Int*) { println(x) }
| }
defined class A
scala> println(classOf[A].getMethods.toList)
List(public void $line1.$read$$iw$$iw$A.foo(scala.collection.Seq), public void $line1.$read$$iw$$iw$A.foo(int[]), ...)
Above, reflection shows that there are 2 versions of method foo
generated - one that takes a Seq[Int]
and another which takes an int[]
.
Source: stackoverflow.com
Related Query
- How to use Scala varargs from Java code
- How to use scala.None from Java code
- How to refer to protected inner class in Scala when inheriting from Java (with byte code only)
- How to declare scala method so that it can be called from Java using varargs style
- How to inject dependencies through Scala Reader from Java code
- How to use Scala annotations in Java code
- How do I use java 8's stream collect from scala 2.11?
- How can I use the Scala REPL to test java code - java and scala REPL giving different outputs
- How to use scala list and java list in the same file in scala code base?
- Use functions from a Scala object that have implicit parameters in java code
- How to compile and run scala code at runtime from a java program?
- How to get Scala List from Java List?
- How to show scala doc from Java Editor in Eclipse?
- Can I use a scala class which implements a java interface from Java?
- How to use scala.collection.immutable.List in a Java code
- Nested Scala singletons from Java code
- Scala - How to compile code from an external file at runtime?
- How to pass Scala Seq to a Java varargs
- How to call main method of a Scala program from the main method of a java program?
- How to use Java Collections.shuffle() on a Scala array?
- How to access a Java static method from Scala given a type alias for that class it resides in
- How to run tests on every code change in IntelliJ IDEA from Scala sbt project?
- How to use Java lambdas in Scala
- How to run JavaScript code from within Scala (JVM)?
- Using Scala type aliases from Java code
- How to use my classes from Scala worksheet in IntelliJ CE with Scala plugin?
- How can I use a combination of Scala, Groovy, and Java code with Gradle?
- NoSuchMethodError when calling java code from scala
- How to use java proxy in scala
- Calling Scala code from Java with java.util.List when Scala's List is expected
More Query from same tag
- Why is this fold incorrect?
- java.lang.RuntimeException: scala.collection.convert.Wrappers$JListWrapper is not a valid external type for schema of string
- How do I store a pointer to a template in scala Play Framework 2?
- Best way to transpose or pivot an array of categorical variables for Spark ML encoding
- Passing and invoking a higher-order function in a macro
- No instance of play.api.libs.json.Format is available for scala.Char in the implicit scope
- How to merge two maps keeping greater of values for matching keys?
- Single infix like method invocation with Scala
- Why do some of my ScalaTest tests using Mockito fail when I add a dependency on Specs2?
- Tunnel implicit parameter to call-by-name function body
- scala playframework returning Ok in flatMap
- Scala - Display the folders if it has files
- Create DataFrame / Dataset using Header and Data in two different directories
- java.net.ConnectException: Connection refused when trying to access a file from HDFS
- Is there a better or functional way to compare 2 values in a map in Scala?
- Iterate through JSON in Scala
- scala reflection error java
- Invalid constant for day-time interval: org.apache.flink.table.api.ApiExpression
- Message 'No configuration setting found for key' Akka-Http
- How to set streaming app checkpointing to Azure storage?
- How to get first satisfied return value for a collection?
- In Table for slick configuration where are only one value tuple doesn't work
- Dynamic languages - which one should I choose?
- find by regular expression with Casbah
- Scala for-loop ranges OutOfMemoryError
- Succinct way to go from List[Any] to a specific case class
- How to make and apply changes to Jan Berkel's android-plugin?
- Junit4 tutorial with pure scala code samples
- How to refer broadcast variable in Spark DataFrameSQL
- scala user define function not working in sparksql