Use case of immutable List

Before going into the reasons to favor immutability, it’s worth pointing out that Scala’s List is a concrete type, implemented as a linked list, while Java’s List is an abstract type with various implementations, so you can’t really compare them directly. For instance, a Java list may very well be a java.util.LinkedList, which is implemented as a doubly-linked list and has o(n) random access, not o(1).

The equivalent of a java List in Scala is a Seq; Vector is the usual array-based (rather than linked-list based) implementation of an immutable Seq, useful if you need, say, fast random access. You can check out the performance characteristics of the standard Scala collections here : https://docs.scala-lang.org/overviews/collections-2.13/performance-characteristics.html.

3 Likes