Scala - Collections

Scala provides a nice collection implementation that provides some abstraction of collection types.

Scala collections are divided into variable and immutable collections.

Variable collections can be updated or extended where appropriate. This means you can modify, add, and remove elements of a collection.

The immutable collection class, by contrast, never changes. However, you can still simulate adding, removing, or updating operations. But these operations will return a new collection in each case, while leaving the original collection unchanged.

Next we will introduce you to several common collection types:


Sr.No Collections with Description

Scala Lists

The characteristic of List is that its elements are stored in a linear manner, and duplicate objects can be stored in the collection.


Scala Sets

Set is the simplest collection. Objects in a collection are not sorted in a specific way, and there are no duplicate objects.


Scala Maps

A Map is a collection of key and value objects, each of which contains a pair of key and value objects.


scala Tuples

Tuples are collections of different types of values


Scala Options

Option[T] represents a container that may contain values or may not contain values.


Scala Iterators

An iterator is not a container, but rather a method of accessing elements within a container one by one.


The following code determines the definition of all the above collection types:

// Definition integer List
val x = List(1,2,3,4)

// definition Set
val x = Set(1,3,5,7)

// definition Map
val x = Map("one" -> 1, "two" -> 2, "three" -> 3)

// Create a tuple of two different types of elements
val x = (10, "welookups")

// definition Option
val x:Option[Int] = Some(5)