How to merge lists in Scala?

Scala | Merging Lists: Here, we are going to learn how to merge given lists in Scala programming?
Submitted by Shivang Yadav, on May 12, 2020

List is a collection of immutable data of the same data type. In Scala, the list represents linked-link data structures.

Syntax to create a new list in Scala,

    val list_name = List(item1, item2, item3...)

Merge lists in Scala

In Scala, we can merge/concatenate two lists into a single list. This will append the second list at the end of the first list.

You have to use a third variable to store the merged list in case of mutable lists. Else, we will have to use the ListBuffer Class(mutable lists) to store the results in the same variable.

Scala programming language provides three different ways to merge a list in Scala,

  1. Using ++ method
  2. Using ::: method
  3. Using concat() method

Merging two lists using ++ method

The ++ method is used to concatenate two lists. It appends the second list at the end of the first list. The ++ method can also be applied to the ListBuffer class i.e. mutable class.

Syntax:

    val list3 = list1 ++ list2

Program to merge two immutable lists in Scala using ++ method

object MyClass {
    def main(args: Array[String]) {
        val list1 = List("C", "C++", "Java")
        val list2 = List("Scala", "Python", "C#")
        
        println("list1 : " + list1)
        println("list2 : " + list2)
        
        println("Merging list1 and list2 ")
        
        val list3 = list1 ++ list2
        
        println("Merged list : " + list3)
    }
}

Output

list1 : List(C, C++, Java)
list2 : List(Scala, Python, C#)
Merging list1 and list2 
Merged list : List(C, C++, Java, Scala, Python, C#)

Program to merge two mutable lists (ListBuffer) in Scala using ++ method

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var list1 = ListBuffer("C", "C++", "Java")
        var list2 = ListBuffer("Scala", "Python", "C#")

        println("list1 : " + list1)
        println("list2 : " + list2)

        println("Merging list1 and list2 ")

        list2 = list1 ++ list2

        println("Merged list : " + list2)
    }
}

Output

list1 : ListBuffer(C, C++, Java)
list2 : ListBuffer(Scala, Python, C#)
Merging list1 and list2 
Merged list : ListBuffer(C, C++, Java, Scala, Python, C#)

Merging two lists using ::: method

The ::: method is also used to concatenate two lists in Scala.

Syntax:

    val list3 = list1 + list2

Program to merge two immutable lists in Scala using ++ method

object MyClass {
    def main(args: Array[String]) {
        val list1 = List("C", "C++", "Java")
        val list2 = List("Scala", "Python", "C#")

        println("list1 : " + list1)
        println("list2 : " + list2)

        println("Merging list1 and list2 ")

        val list3 = list1 ::: list2

        println("Merged list : " + list3)
    }
}

Output

list1 : List(C, C++, Java)
list2 : List(Scala, Python, C#)
Merging list1 and list2 
Merged list : List(C, C++, Java, Scala, Python, C#)

The ::: method cannot be applied to ListBuffer class.

Merging two lists using concat() method

The concat() method is List class which is used to concatenate two lists in Scala.

Syntax:

    val list3 = list.concat(list1, list2)

Program to merge two immutable lists in Scala using concat() method

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var list1 = ListBuffer("C", "C++", "Java")
        var list2 = ListBuffer("Scala", "Python", "C#")

        println("list1 : " + list1)
        println("list2 : " + list2)

        println("Merging list1 and list2 ")

        val list3 = List.concat(list1, list2)

        println("Merged list : " + list3)
    }
}

Output

list1 : ListBuffer(C, C++, Java)
list2 : ListBuffer(Scala, Python, C#)
Merging list1 and list2 
Merged list : List(C, C++, Java, Scala, Python, C#)



Comments and Discussions!

Load comments ↻






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