×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Collections in Scala

By IncludeHelp Last updated : November 07, 2024

Introduction to Collections in Programming

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.

Collections in Scala

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

Mutable and Immutable Collections

1. Mutable Collections

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

2. Immutable Collections

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.

Lazy and Strict Collections

Based on how they are used, collections in Scala can be lazy or strict:

  • Lazy Collections: These may not consume memory space until accessed by the program.
  • Strict Collections: These are evaluated immediately, consuming memory as they are created.

Collection Hierarchy in Scala

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.

Commonly Used Collections in Scala

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;

1. 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.

val cities = List("New York", "Paris", "Tokyo", "Sydney")

Read More: Scala Lists

2. 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.

val cityInfo = ("Paris", "France", 2148000)

Read More: Scala Tuples

3. 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.

val cities = Set("New York", "Paris", "Tokyo", "Paris")

Read More: Scala Sets

4. Scala Map

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

val countryCapitals = Map("France" -> "Paris", "Japan" -> "Tokyo", "USA" -> "Washington, D.C.")

Read More: Scala Maps

Comments and Discussions!

Load comments ↻





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