简体   繁体   中英

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

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 (eg 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).

This doesn't specifically answer your question, but for general use I don't think you would notice a difference between java and scala collections. I don't notice it at least :) In general I think that people prematurely optimise by worrying about these kinds of things (although you may have a perfectly valid use case that you need optimisation for).

Personally I prefer using scala collections over java collections because of their APIs, and because I have never had an issue with performance, I have never really looked into the java vs scala benchmarks.

As Rex says though, if you are worried about it (or just wondering), setting up a benchmark on your specific use cases would be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM