Choosing a Collections Class (Scala 3 Video)
Tips
- If you’re coming to Scala from Java, forget the Java collections classes. Using them will just slow you down.
Vector
andArrayBuffer
are your “go to” sequence types.
Immutable vs Mutable
The collections types can be classified as immutable (unchangeable) and mutable:
- Immutable type are available by default (
Vector
,List
, and the immutableMap
andSet
)- Immutable is preferred (always use immutable, unless there’s a good reason not to)
- Mutable types have to be imported to use them
Sequences are Indexed or Linear
Sequence types can also be classified as indexed or linear:
- Indexed
- Fast, random access to elements
Vector
,Seq
,ArrayBuffer
- Linear
- i.e., a linked list (
List
) - Random access is slow
- i.e., a linked list (
The “Go To” (common) data types
Vector
andArrayBuffer
are the preferred sequence types.List
is also okay for small, immutable sequences (or when you are prepending elements and accessing the head and tail components).
Immutable | Mutable
----------------------------
List, Vector | ArrayBuffer
Map | Map
Set | Set
Common types
Vector
is an immutable, indexed sequenceList
is an immutable, linear sequenceArrayBuffer
is a mutable, indexed sequence- We use other types like
Map
,Set
,Range
, and tuples all the time.
Other:
- There is an
Array
, which wraps the Javaarray
, but we don’t really use it directly (useArrayBuffer
instead)
When returning types from functions:
Seq
implies immutableIndexedSeq
implies immutable, indexed
Creating collections instances is very consistent:
val a = Vector(1, 2, 3)
val b = List(1, 2, 3)
val c = ArrayBuffer(1, 2, 3)
val d = Set(1, 1, 1, 2, 2, 2, 3)
val e = Map(1 -> "a", 2 -> "b")
Using them in for
expressions is also consistent, as are append, prepend, update, and delete methods. (See the Vector
and ArrayBuffer
videos for examples.)
Map
- A
Map
is a list of key/value pairs, where the keys are unique. - The immutable
Map
is available by default. - You have to import the mutable
Map
if you want to use it.
val a = Map("a" -> 1, "b" -> 2)
val m = Map(
"first_name" -> "Alvin",
"last_name" -> "Alexander"
)
Set
- A
Set
is a collection of unique elements. - The immutable
Set
is available by default. - You have to import the mutable
Set
if you want to use it.
val set = Set(1, 1, 2, 2, 3, 3, 3)
// result: Set(1, 2, 3)
Range
- A
Range
is a sequence of equally spaced values. - Typically created with
to
(anduntil
).
1 to 10
1 to 10 by 2
'a' to 'e'
'a' to 'e' by 2
for i <- 1 to 10 yield i * 10
Tuple
- Not really a collection class, but similar.
- A heterogenous collection of values.
(1, 2.2, 3L, 'a', "yo") // (Int, Double, Long, Char, String)
val t = (1, 2.2, 3L, 'a', "yo")
t(0)
t(1)
def getUserInfo(): (String, Int, Double) =
// do some stuff here, then return a 3-element tuple
("Al", 42, 200.0)
val (name, age, weight) = getUserInfo()
See the “Tuples” video for more details.
More resources
Detailed blog posts on my website, with hundreds of examples:
Update: All of my new videos are now on
LearnScala.dev