Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java -> Scala, performances on collections

In Java, according to the usage of a collection, we do not use the same implementation (ie ArrayList vs LinkedList).

Coming from a Java background, can someone tell me what should I know about Scala collections and performance considerations?

It seems the immutable version of Scala List is some kind of immutable LinkedList. I understand the concepts, following Martin Odersky courses on Coursera. In the same way i understand why it's more efficient to prepend than to append, particularly when you have an immutable list.

I would like to know all (or most) of these kind of tricky things about Scala collection performances, so that I do not find it myself the hard way. Can someone help me?

Thanks

like image 848
Sebastien Lorber Avatar asked Oct 20 '12 00:10

Sebastien Lorber


People also ask

Is Scala as fast as Java?

Scala vs Java performance Some coders claim that Scala is a little bit faster than Java programming with 20% fast processing. Optimization in Scala language makes code compilation much faster than Java coding.

Can you use Scala libraries in Java?

Combine Scala and Java seamlessly Similarly, Java code can reference Scala classes and objects. In this example, the Scala class Author implements the Java interface Comparable<T> and works with Java File s. The Java code uses a method from the companion object Author , and accesses fields of the Author class.

Can I use Java list in Scala?

A java list can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don't even need to import any Scala's JavaConversions object in order to make this conversions work.

Does Scala have immutable collections?

A collection in package scala. collection can be either mutable or immutable.


1 Answers

There's a document that describes collection performance characteristics. Beyond that, you really should test your use case in a microbenchmark. In some cases, Scala collections are very close in performance to Java ones; in others there's a gap (e.g. maps); in others there is no Java analog and the immutable vs. mutable comparison depends very highly on how you use the collection (with, obviously, heavy mutation favoring mutable collections, and heavy re-use/copying favoring immutable collections).

like image 166
Rex Kerr Avatar answered Sep 22 '22 04:09

Rex Kerr