score:6
as others pointed out, you can easily use java classes in scala. gyro gearless and maasg showed how can you use scala's library to do guava's work in your sample. you can use guava as well obviously, here's how you can transform you sample to scala more straightforwardly:
import com.google.common.base.joiner
import scala.collection.javaconverters._
object mainj extends app {
val fantasygeneres = array("space opera", "horror", "magic realism", "religion")
val joined = joiner.on(',').join(fantasygeneres.toiterable.asjava)
println(joined)
}
note that i used javaconverters
to convert scala's iterable
to java's version, but that's the only thing that can be considered as inconvenience here.
score:4
yes, you may transparently use java classes in scala. but very often there is an idiomatic way to express things more elegantly in scala. for your given example this might be:
c:\opt>scala
welcome to scala version 2.10.0 (java hotspot(tm) 64-bit server vm, java 1.7.0_25).
type in expressions to have them evaluated.
type :help for more information.
scala> val fantasygenres = list("space opera", "horror", "magic realism", "religion")
fantasygenres: list[string] = list(space opera, horror, magic realism, religion)
scala> val joined = fantasygenres.mkstring(",")
joined: string = space opera,horror,magic realism,religion
score:9
according to scala faq (emphasis added):
the standard scala backend is a java vm. scala classes are java classes, and vice versa. you can call the methods of either language from methods in the other one. you can extend java classes in scala, and vice versa. the main limitation is that some scala features do not have equivalents in java, for example traits.
do i need to convert java data structures to scala, and vice versa?
you do not have to convert java data structures at all for using them in scala. you can use them "as is". for instance, scala classes can subclass java classes, you can instantiate java classes in scala, you can access methods, fields (even if they are static), etc.
Source: stackoverflow.com
Related Query
- Properly use Java libraries in Scala
- How to use Java libraries asynchronously in a Scala Play 2.0 application?
- How to use java libraries with scala using sbt?
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- How to use third party libraries with Scala REPL?
- Using Java libraries in Scala
- How to use Scala varargs from Java code
- One Play 2 Framework App - use both java and scala
- Can I use a scala class which implements a java interface from Java?
- Best practices with Akka in Scala and third-party Java libraries
- To use or not to use Scala for new Java projects?
- How to use Java Collections.shuffle() on a Scala array?
- How to use Java lambdas in Scala
- Scala 2.8: use Java annotation with an array parameter
- Use Scala constants in Java
- Can I use java 8 in an mixed scala 2.10 / java project built by sbt?
- How to use java proxy in scala
- Why can I use a Java lib from the Scala REPL but not from a script?
- How to use Scala implicit class in Java
- Cannot use Scala class in Java
- Can I use groovy templating instead of scala for Play 2.0 based Java applications?
- Simple use of Scala collections from Java not compiling with 2.11
- SBT: how to use scala 2.11 libraries in scala 2.12 project
- How to use java Map<String, Object> where scala Map[String, Any] is requred?
- Can we use scala to debug Java programs
- Scala wrappers for any of Java statistics libraries
- Can I use the new java 8 time API within a scala Play 2.3.8 project?
- Scala working with Java libraries not specifying collections' type parameters
- Cannot use java value as parameter to java annotation on scala function
- How to include Java libraries to scala project
More Query from same tag
- Scala tuple memory overhead
- How to convert scala.collection.mutable.Map to scala.collection.immutable.Map in Kotlin?
- How to count number of columns in Spark Dataframe?
- sbt not resolving dependency; path correct except ${package.type} extension
- Scala Spark - Discard empty keys
- Asynchronous http requests using Netty and Scala actors
- Converting command line argument key=value pair to Map in scala
- JSON to Scala code generation
- Parsing RDF items
- Convert a document with Play Json
- Use combineByKey to get output as (key, iterable[values])
- Can app use a dependency lib which has been developed lower scala version?
- Fill ArrayBuffer with pairs of Double
- What is twitter's Future.trace() method used for?
- Why AWS is rejecting my connections when I am using wholeTextFiles() with pyspark?
- Type erasure in a nested list with a given context bound
- Running Applications doesn t appear spark web Ui but runs
- How to compare a column with the columns in the same dataframe in apache spark
- Using a Future's response
- Why subclass's instance declared as parent type doesn't have its additional members in Scala?
- Idiomatic search of value in a list of element-value pairs in Scala
- How to rate limit function call in a loop in Scala
- Scala same fallback for main if and and failure in a Try() call inside the true block
- SBT: Accessing setting values of subprojects in scala build
- How do I intercept all controller requests in Play 2 + Scala?
- Succinct way of reading data from file into an immutable 2 dimensional array in Scala
- Scala: implementing Map with concrete types
- HList foldRight is working, but foldLeft does not compile
- Does Await have overhead in test case
- Scala: http4s giving 401 Unauthorized for same request that works in curl/requests