How to merge lists in Scala

A list is a collection which contains immutable data. List represents linked list in Scala. The Scala List class holds a sequenced, linear list of items. Lists are immutable and represents a linked list.

Syntax of List:

val variable_name: List[type] = List(item1, item2, item3)
or
val variable_name = List(item1, item2, item3)

Below are three different way to merge lists:



  • using the ++
  • Using :::
  • Using concat

Following are the various ways to merge two lists in Scala:

By using ++ method

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to merge lists
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        // Creating Lists 
        val a = List("geeks", "for", "geeks"
        val b = List("is", "a", "computer science", "portal")
        val c = List("for", "geeks")
          
        // Merging Lists
        val d = a ++ b ++ c
        println("After merging lists "
        println(d) 
    
}     
     

chevron_right


Output:

After merging lists 
List(geeks, for, geeks, is, a, computer science, portal, for, geeks)

In above example we are using ++ method to concatenate lists. This method is used consistently across immutable collections  

By using :::

If we using the List class frequently, we may prefer using ::: method.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to merging two lists
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a List. 
        val a = List("Languages", "are"
        val b = List("C++", "Java", "C#", "Python", "Scala")
          
        // Merging two lists
        val c = a ::: b
          
        println("After mergeing lists"
        println(c) 
    
}     
     

chevron_right


Output:

After mergeing lists
List(Languages, are, C++, Java, C#, Python, Scala)

 

By using concat method

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program merging two lists
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a List. 
        val a = List(1, 2, 3
        val b = List(4, 5, 6)
          
        // concatenate two lists
        val c = List.concat(a, b)
        println("After merging lists"
        println(c) 
    
}     

chevron_right


Output:

After merging lists
List(1, 2, 3, 4, 5, 6)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.