score:6

Accepted answer

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.


Related Query

More Query from same tag