How to Add Element in an Immutable Seq in Scala? Last Updated : 02 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn how to add elements in immutable seq in Scala. immutable sequences, such as List, cannot have elements added to them directly because they are designed to be immutable. However, you can create a new sequence with an additional element by using methods like :+ (for appending) or +: (for prepending), which returns a new sequence with the added element. Syntax: For Appending: originalList :+ 4 For Prepending: 0 +: originalList Example 1: Below is the Scala program to add numbers in an immutable sequence: Scala // Original immutable sequence val originalList = List(1, 2, 3) // Adding an element to the end of the sequence val modifiedList = originalList :+ 4 // Adding an element to the beginning of the sequence val modifiedList2 = 0 +: originalList // Printing modified lists println(modifiedList) println(modifiedList2) Output: Explanation: :+ appends an element to the end of the list.+: prepends an element to the beginning of the list.Example 2: Below is the Scala program to add words in an immutable sequence: Scala // Original immutable sequence val originalSeq = Seq("apple", "banana", "orange") // Adding an element to the end of the sequence val modifiedSeq = originalSeq :+ "grape" // Adding an element to the beginning of the sequence val modifiedSeq2 = "melon" +: originalSeq // Printing modified sequences println(modifiedSeq) println(modifiedSeq2) Output: Explanation: :+ appends an element to the end of the sequence.+: prepends an element to the beginning of the sequence. Comment More infoAdvertise with us Next Article Methods to call on a Set in Scala R raushanikuf9x7 Follow Improve Article Tags : Scala Similar Reads How to access list elements in Scala? In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possibl 2 min read Scala Mutable SortedSet map() method In Scala mutable collections, map() method is utilized to build a new SortedSet by applying a function to all elements of this SortedSet. Method Definition: def map[B](f: (A) => B): mutable.SortedSet[B] Return Type: It returns a new SortedSet containing all the elements after applying the given f 1 min read Methods to call on a Set in Scala A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala. 13 min read Scala Mutable SortedSet +() method In Scala mutable collections, +() method is utilized to create a new SortedSet with an additional element unless the element is already present. Method Definition: def +(elem: A): SortedSet[A] Return Type: It returns a new SortedSet with an additional element unless the element is already present. E 1 min read Scala Mutable SortedSet toSeq() method In Scala mutable collections, SortedSet toSeq() method is utilized to return a seq consisting of all the elements of the SortedSet. Method Definition: def toSeq: Seq[A] Return Type: It returns a seq consisting of all the elements of the SortedSet. Example #1: Scala // Scala program of toSeq() // met 1 min read Scala mutable SortedSet ++() method In Scala mutable collection, ++() method is utilized to add elements of one SortedSet to another SortedSet. Method Definition: def ++(elems: A): SortedSet[A] Return Type: It returns a new TreeSet containing elements of both the SortedSets. Example #1: Scala // Scala program of ++() method import sca 1 min read Like