Exploring Scala Lists: From Basics to Advanced Usage

Topic Overview and Actualization

Greetings, coding enthusiasts! Today, we're diving into Scala's diverse toolbox to unravel a fundamental data structure: Lists! Much like a shopping list or a task list, Scala allows us to create a list of items. With our eyes fixed on mastering Lists, we're geared up for a journey into the world of Scala!

Introduction to Lists in Scala

Have you ever created a to-do list? It stores your tasks in one place, right? Likewise, Scala's Lists can store multiple items of homogeneous types. There are two primary types of lists in Scala: immutable and mutable.

Immutable lists, once created, cannot be changed. This means you can't update existing elements, add new elements, or remove elements from an immutable list. On the other hand, mutable lists can be modified — you can add, change, and remove elements. Consider this example of a to-do list:

Scala
@main def run: Unit =
  val myToDoList = List("buy groceries", "take out the trash", "send emails")
  println(myToDoList)  // This will print the list in a nicely formatted way

By default, Lists in Scala are immutable. If you need mutable lists, Scala provides ListBuffer, which we'll explore in this lesson.

Creating Lists in Scala

To create a list in Scala, you can use the List() function. This will create an immutable list. For mutable lists, you can use ListBuffer by importing scala.collection.mutable.ListBuffer:

Scala
import scala.collection.mutable.ListBuffer

@main def run: Unit =
  val names = List("Alice", "Bob", "Charlie")  // An immutable list
  println(names)  // Prints List(Alice, Bob, Charlie)

  val mutableNames = ListBuffer("Alice", "Bob", "Charlie")  // A mutable list
  mutableNames += "Dave"  // Adding an element to the mutable list
  println(mutableNames)  // Prints ListBuffer(Alice, Bob, Charlie, Dave)

The import statement import scala.collection.mutable.ListBuffer is necessary to tell Scala to include the ListBuffer functionality from its library. Without this, Scala wouldn't recognize ListBuffer as a valid data structure.

Working with Immutable Lists

You can access elements in an immutable list using numeric indices. Consider that list indices in Scala start at 0:

Scala
@main def run: Unit =
  val fruits = List("Apple", "Banana", "Cherry")
  println(fruits(0))  // Accessing the first element, "Apple"
  println(fruits(2))  // Accessing the third element, "Cherry"

Note that index values should be within the range of 0 to list.length - 1. If you try to access an element with an invalid index, Scala will throw an IndexOutOfBoundsException:

Scala
@main def run: Unit =
  val fruits = List("Apple", "Banana", "Cherry")
  println(fruits)  // Prints List(Apple, Banana, Cherry)
  println(fruits(5))  // This will throw an IndexOutOfBoundsException
  println(fruits(-3))  // This will throw an IndexOutOfBoundsException

Even though we mentioned that it's not possible to add elements to immutable lists, it's possible to create a new list with additional elements. You can use the :+ or ++ operators to achieve this. These operations will return a new list and keep the original list unchanged:

Scala
@main def run: Unit =
  val numbers = List(1, 2, 3)
  println(numbers)  // Prints List(1, 2, 3)
  val moreNumbers = numbers :+ 4  // Appending one element, returns a new list
  println(moreNumbers)  // Prints List(1, 2, 3, 4)
  val evenMoreNumbers = numbers ++ List(4, 5, 6)  // Appending multiple elements, returns a new list
  println(evenMoreNumbers)  // Prints List(1, 2, 3, 4, 5, 6)
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal