-5

What are the main differences between Java collections and Scala collections? Why the Scala creators decided to create a whole new set of classes instead of building on top of the existing ones?

Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
  • The question is, Why did they decide to re-implement Scala collections again for 2.13? As posed, a possible duplicate of http://stackoverflow.com/questions/1722726/is-the-scala-2-8-collections-library-a-case-of-the-longest-suicide-note-in-hist – som-snytt Oct 26 '16 at 20:09
  • There are currently three implementations of Scala: Scala-JVM, Scala.js, Scala-native. In the past, there was also Scala.NET. 3 of those 4 cannot possibly use Java Collections. – Jörg W Mittag Oct 27 '16 at 00:46

3 Answers3

4

Java standard lib does not have immutable data structures.

Scala contains both mutable and immutable data structures unlike java which has only mutable data structures (mostly).

Scala collections gives interop between mutable and immutable collections. Supports lazy collections.

Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
3

For my technical take, From the scala-lang.org website, they say:

Scala libraries put much more emphasis on immutable collections, and provide many more operations that transform a collection into a new one.

More info for an overview of scala collections. Also, performance characteristics on scala collections (originally found from this SO post).

Like much of scala, the collections is also more implicit and covers repetitive boiler plate code.

The Scala cookbook also explains more in detail about not only the conversion of java<->scala conversions, but many aspects of scala and java over a wider variety of code.

As for why I cannot claims any definitive reasons except for my own simple logical understanding of that: it enables the scala language to flourish in a more cohesive manner, e.g. functional programming, as opposed to java and OOP. This kind of ties into @pamu's answer too.

Community
  • 1
  • 1
Nick Bell
  • 516
  • 3
  • 16
2

Part of the answer is already given by the other answers (immutable aka purely functional data structures). Here are a few more advantages:

  • easy to write pattern matching and recursive functions, e.g. using head :: tail decomposition for List, or init :+ last decomposition for sequences.
  • easy to convert between collections, e.g. sequence to map or set and back
  • easy to change from var and immutable variant to val and mutable variant (e.g. map += key -> value).
  • contains a large amount of easily usable higher order functions, such as map, groupBy, filter etc. I don't think theses are available in Java (perhaps in Java 8 with streams).
  • platform independent (works on the JVM and in JS back-ends, and probably in Scala Native as well)
  • play nicely with other Scala types such as Option (there are a few exceptions where only unsafe methods exist, such as max).
0__
  • 66,707
  • 21
  • 171
  • 266