How to add elements to a List in Scala with examples

13 total views, 1 views today

Welcome to Scala series. In this tutorial, you will learn how to add elements to a List in Scala with examples.

List is a collection which used to stored values in an orderly manner. In Scala, the List is always an immutable data structure. You cannot add an element to a list. When you tried to add an element, you will generate a new result out of it but the original list will not be modified.

List is an immutable collection in Scala

Please visit immutable and mutable collections to learn more about it.

Let us now see a few examples of adding elements to a List which us by default immutable. i.e ., the Source list will not be modified. When you learn each example, you would be noticing that each operation on the list generates a new list.

1. Add an element in the list(Append in the rear)

In this example, we are trying to add an element to the rear end of the Scala list.

object StaticReference {
  def main(args: Array[String]): Unit = {

    var list: List[String] = List("a", "b", "c", "d")
    var result = list.++("e")
    result.foreach(println _)

  }
}

Output

a
b
c
d
e

2. Add an element in the list(Prepend in the front)

In this example, we are trying to add an element to the front of the list. i.e., as ahead of the Scala list.

object StaticReference {
  def main(args: Array[String]): Unit = {

    var list: List[String] = List("a", "b", "c", "d")
    var result = list.::("e")
    result.foreach(println _)

  }
}

Output

e
a
b
c
d

3. Add/merge a list to another list(Append in the rear)

In this example, we are trying to add another list to the rear end of the list. i.e., we are trying to merge both list by adding the other list to rear end.

object StaticReference {
  def main(args: Array[String]): Unit = {

    var list: List[String] = List("a", "b", "c", "d")
    var result = list.++(List("x", "y"))
    result.foreach(println _)

  }
}

Output

a
b
c
d
x
y

4. Add/merge a list to another list(prepend in the front)

In this example, we are trying to add another list to the front of the list. i.e., we are trying to merge both list by adding the other list to the front or prepending.

object StaticReference {
  def main(args: Array[String]): Unit = {

    var list: List[String] = List("a", "b", "c", "d")
    var result = list.:::(List("x", "y"))
    result.foreach(println _)

  }
}

Output

x
y
a
b
c
d

Okay, So what happens to source when we add an element. Let us see an example.

object StaticReference {
  def main(args: Array[String]): Unit = {

    var list: List[String] = List("a", "b", "c", "d")
    list.:::(List("x", "y"))
    list.foreach(println _)

  }
}

Output

a
b
c
d

From the above example, it is clear that source is UNCHANGED. So List in Scala is always immutable.

So how do you achieve mutability by using Scala List?

In this case, you have to use ListBuffer, ArrayBuffer, LinkedList. Based on your needs or use case, you can choose either of these and achieve mutability.

If you would like to learn more about other Scala List operations, please visit here.

References: Scala List official documentation

I hope you like this tutorial and you were able to understand how to add elements to a List in Scala with examples.

Thanks for reading and please give us a thumb up and comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.