Vulcann
Vulcann

Reputation: 177

Difference on concatenation for immutable list between java Guava and Scala

I am trying spark with java and I get stuck by the immutable collections in java.

As I understand in Scala when two immutable lists are combined, no deep copy happens. However the available immutable list in java, like guava, does the defensive copy. (correct me if I am wrong)

So simply my questions are:

  1. Is there some Java immutable list which has the same behavior as scala immutable list ?
  2. If the answer of the first question is NO, what's the general(standard) way to use scala immutable collection in java code ?

Thanks very much.

Upvotes: 0

Views: 371

Answers (1)

knutwalker
knutwalker

Reputation: 5974

Scala Lists is a so-called persistent collection with the persistent referring to the fact, that no defensive copying happens. If you google for Java persistent collection you should find several links to get you started. In addition to that, there are a number of libraries that aim to bring the essence of functional programming to Java. As persistent collections are inherently functional, those frameworks often include their own collections implementation. The two libraries I've worked with and can recommend are Javaslang and Functional Java but there are more out there than these two (e.g. pccollections, jOOλ, …).

As for using Scala collections from Java, I've always found this to be somewhat awkward. I would suggest to write a Scala class/object, that gives you easy (as from-Java-easy) access to the scala.collection.JavaConverters, so that you can expose your scala.collection.immutable.List as a java.util.List and work with that interface.

Upvotes: 3

Related Questions