When choosing a collection in Scala, one needs to be aware of their requirements as different collections have different performance characteristics. This way, a user can pick the most optimized choice based on their requirements. The two tables below define the relevant collections and the complexity of using their methods.
Please take a look at the key before looking at the tables for further clarity.
C: This symbol means that the mentioned operation takes constant time, , to complete.
eC: This symbol means that the operation ideally takes a constant time, , to complete. However, the time complexity is subject to change based on the length of a list or type of hashmap.
aC: This symbol means means that, on average, the operation takes constant time . However, this operation can take
Log: This symbol means the specified operation takes logarithmic time, , to complete.
L: This symbol means that the operation takes linear time, , to complete.
-: This operation is not supported.
An immutable object’s value cannot be changed; while, mutable objects allows the values associated to be changed.
A map stores key-value pairs. Given a key, a value is returned (if the key is present). For a set, it also stores key-value pairs. However, the sets return a boolean
False that indicate whether or not a key is present.
Let’s have a look at what the methods in these two tables represent:
head: Accesses the first element.
tail: Gives a new sequence without the first element.
apply: Performs indexing on data.
update: Functionally updates sequences.
prepend: Adds an element at the
append : Adds an element at the end (as the last element) of a sequence.
insert: Adds/inserts a value at any arbitrary position.
lookup: Searches for if a certain element exists in a set.
add: Adds a new element to a set or a key/value pair to a map.
remove: Removes an element form set – it can also be used to remove a key from a map.
min: Accessse the minimum smallest element/key in the set/map.
For further information, visit the official documentation.
View all Courses