scala list addall
In Scala, the addAll
method is used to add multiple elements from one collection to another collection. The addAll
method is available on mutable collections such as ListBuffer
, ArrayBuffer
, and MutableList
. It allows you to append all elements from another collection to the end of the current collection.
Here's an example of how to use the addAll
method in Scala:
import scala.collection.mutable.ListBuffer
// Create a ListBuffer
val listBuffer1 = ListBuffer(1, 2, 3)
// Create another ListBuffer
val listBuffer2 = ListBuffer(4, 5, 6)
// Use addAll to append elements from listBuffer2 to listBuffer1
listBuffer1.addAll(listBuffer2)
// Print the updated listBuffer1
println(listBuffer1)
This will output:
ListBuffer(1, 2, 3, 4, 5, 6)
In the above example, we first create two ListBuffer
instances named listBuffer1
and listBuffer2
. We then use the addAll
method on listBuffer1
to append all elements from listBuffer2
. Finally, we print the updated listBuffer1
to verify that the elements have been added successfully.
You can use the addAll
method with other mutable collections in Scala in a similar way to add multiple elements to a collection.