Appending an Element at the end of List in Scala

Here, we will learn about how to add a new element to the end of a List in Scala? We will see examples and methods to add elements to the end of list.
Submitted by Shivang Yadav, on December 23, 2020

Lists in Scala is a collection that stores data in the form of a liked-list. In Scala, list is immutable i.e., cannot be altered. For that Scala has List-Buffer.

We can update a list and add new element to the list in multiple ways.

Out of all the methods we have learned not all method can be used to append elements to the end.

Methods:

1) Using :+ method

The :+ method in Scala is used to append element to the end of list.

Syntax:

list_Name :+ element_Name

Program to add an element to the end of list

// Program to illustrate appending of element 
// at the last of list

object MyClass {
    def main(args: Array[String]) {
        val myList = List ("C++" , "Scala ")
        println("Content of list : " + myList)
        
        println("Appending element to the end of list...")
        // adding element to the last of list 
        val updatedList = myList :+ "JavaScript"
        println("Content of updated list : " + updatedList)
    }
}

Output:

Content of list : List(C++, Scala )
Appending element to the end of list...
Content of updated list : List(C++, Scala , JavaScript)

In the above code, we have used the :+ method to append element to the list. We have created a list of strings named myList and then printed its value. After this, we have updated the list using :+ method and then initialized it to a new variable updatedList. And at last, printed the value of it.

2) Using list concatenation operator (:::)

The ::: method is used to concatenate two lists. Using this we can add an element at the end of list by taking the second list as a single element list.

Syntax:

list_name_1 ::: list_name_2

Program to illustrate adding of element at the end of list using list concatenation method

// Program to illustrate appending of element at the last of 
// list using list concatenation...

object MyClass {
    def main(args: Array[String]) {
        val list1 = List ("C++" , "Scala")
        println("Content of list 1 : " + list1)
        
        val list2 = List ("javaScript")
        println("Content of list 2 : " + list2) 
        
        println("Appending element to the end of list...")
        //adding element to the last of list 
        val updatedList = list1 ::: list2
        println("Content of updated list : " + updatedList)
    }
}

Output:

Content of list 1 : List(C++, Scala)
Content of list 2 : List(javaScript)
Appending element to the end of list...
Content of updated list : List(C++, Scala, javaScript)

Here, we have created two list list1 and list2. The list2 has only one element. Then we have concatenated both of the lists. And then printed the updated list.




Comments and Discussions!

Load comments ↻






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