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
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.
By using concat method
After merging lists List(1, 2, 3, 4, 5, 6)