Understanding Scala CollectionsTuesday, Jan 08, 2019
Scala Collection Classes exist in Package “scala.collection” or one of its sub-packages.
And, I would like to discuss the following points about Scala Collections:
- Collections Hierarchy
- Choosing the suitable Sequence Collection
- Collection Methods
Collection Classes start from Traversable trait (it is something near to Java Interface). Then, as shown in (Figure 1), it is divided into three categories:
- Sequence: it is a collection of indexed or linear elements (linear means like a linked list, but indexed means I can access any element directly using its index).
- Map: it is a collection of key/value pairs. It is like a dictionary.
- Set: it is a unique collection of elements (no duplication).
Choosing the Suitable Sequence Collection
In order to choose the suitable sequence, you have to think about two important aspects:
- Is it Immutable or Mutable?
In general, immutable sequence list will not allow you to modify its items once it has been added. Also, it has a fixed size.
- Is it Linear or Indexed?
Indexed allows you to access the item directly by their index (random accessed).
The below table lists the Immutable/Mutable Sequences (Linear or Indexed) with descriptions:
Scala Collections provide us with a lot of methods that can manipulate data. Most of the methods take either a function or predict (a function that take one or more parameters and return Boolean).
Most common methods are:
- Collect: return a new collection by applying a function to each element of the old collection
- Count: return number of element
- Diff: return the difference between two collection
- Filter: take predict and return all elements that make the predict true
- Find: return first element that makes the predict return true
- Foreach: apply function to all element in the collection
- groupBy: group collection elements into a Map of Collections
- head: return first element in the collection
- isEmpty: return true if the collection is empty
- last: return last element in the collection
- max: return the maximum element in the collection
- product: return the multiplication of the element in the collection
- take: return the first n element from collection
- tail: return all element except the first one
- union: Returns all elements of two collections.
For some cases, it is better to use mutable collections. For others cases, immutable collections are better. When you are not sure, it is better to try immutable collection first, then use mutable if needed.