Append or Prepend Elements to Sequence in Scala

  1. Use the :+ Operator to Append an Element to a Sequence in Scala
  2. Use ++ to Append Multiple Elements to Sequence in Scala
  3. Use +: to Append an Element to a Sequence in Scala
  4. Use ++: to Prepend Multiple Elements to a Sequence in Scala
  5. Conclusion

This article will teach how to append or prepend elements to a Sequence in Scala.

The sequence is a collection that is a part of the iterable class. And it is immutable by nature, which means we cannot modify it.

Since they are immutable, the result of appending or prepending elements to a sequence should be assigned to a new variable.

There are multiple ways to append or prepend elements to a sequence; let’s see them one by one.

Use the :+ Operator to Append an Element to a Sequence in Scala

Scala provides the :+ operator to append a single element to the sequence or a vector.

Syntax:

var temp = our_seq :+ element

Example code:

object MyClass {

    def main(args: Array[String])
    {
         val a = Seq("Apple", "Orange", "Mango")

        val temp = a :+ "Watermelon"

         println(temp)
    }
}

Output: We can see the string Watermelon is appended.

List(Apple, Orange, Mango, Watermelon)

Use ++ to Append Multiple Elements to Sequence in Scala

In Scala, we can use the ++ operator to append multiple elements to the sequence or a vector.

Syntax:

var temp = seq_one ++ collection_of_elements

The collection of elements could mean another vector or sequence or list.

Example code:

object MyClass {

    def main(args: Array[String])
    {
         val fruits = Seq("Apple", "Orange", "Mango")

         val vegetables = Seq("Onion","tomato","potato")

        val temp = fruits ++ vegetables

         println(temp)
    }
}

Output: In the above code, we are appending the elements of sequence vegetables to sequence fruits.

List(Apple, Orange, Mango, Onion, tomato, potato)

Use +: to Append an Element to a Sequence in Scala

We can use the +: operator to append an element to a sequence or vector in Scala.

Syntax:

val temp = element +: sequence

Example code:

object MyClass {

    def main(args: Array[String])
    {
         val fruits = Seq("Apple", "Orange", "Mango")

        val temp = "dragonFruit" +: fruits

         println(temp)
    }
}

Output: We can see the string dragonFruit is prepended.

List(dragonFruit, Apple, Orange, Mango)

Use ++: to Prepend Multiple Elements to a Sequence in Scala

In Scala, we can use the ++: operator to prepend multiple elements to the sequence or a vector.

Syntax:

var temp = collection_of_elements ++: seq_one

The collection of elements could mean another vector or sequence or list.

Example code:

object MyClass {

    def main(args: Array[String])
    {
         val fruits = Seq("Apple", "Orange", "Mango")

         val data = Vector(1,2,3,4,5)

        val temp = data ++: fruits

         println(temp)
    }
}

Output: In the above code, we are prepending the elements of Vector data to sequence fruits.

List(1, 2, 3, 4, 5, Apple, Orange, Mango)

Conclusion

We have seen multiple ways to append or prepend elements to a sequence, and these same methods work even for a vector.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Scala Sequence

  • The Difference Between Sequence and List in Scala