Home » Scala language

Collections in Scala

Scala Collections: A collection in programming is simply an object that stores data. Scala has a rich set of collection libraries. In this Scala tutorial on collections, we will learn some basics of Scala collections.
Submitted by Shivang Yadav, on July 12, 2019

Scala collections

A collection in programming is a simple object used to collect data. It groups together elements into a single entity (object). You can do the operation is to add, delete, update data using collections.

In Scala, there is a huge base of collection libraries. There is a wide hierarchy of collections in Scala. Basic collections contain linear sets.

The collection in Scala can be mutable as well as immutable.

A mutable collection is a collection whose elements can be updated and elements are added or removed from it. It allows all these operations.

An immutable collection does not allow the user to do the update operation or add and remove operation on it. There is an option to do this, but on every operation, a new collection is created with updated value and the old one is discarded.

Based on how they have used the collections are classified as lazy or strict. If created as a lazy collection, it may not consume memory space until they are called by the program.

As the hierarchy is concerned, we have a list of collections, with Traversable trait, it allows you to traverse the collection i.e. visit each element in the collection one by one. Traversable has followed another trait Iterable that allows iterating over the elements of the collection.

Here we are listing some commonly used collections that you will encounter in your Scala programming life. There is a huge base that supports them, this is just an overview;

Scala List

The list is a linear array-like collection. It stores a similar type of elements and uses a linked list type storage method. List is an immutable collection that extends linearSeq trait.

Scala Tuple

Tuple in Scala is an immutable collection. It can contain a fixed number of elements of different types i.e. it can hold an integer and string together.

Scala Set

A set is a type collection that contains elements of the same data type, it does not contain any duplicate elements. Set can mutable as well as immutable in Scala.

Scala Map

A map is a collection that contains key-value pairs. To retrieve the value the key is used.

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.