29 Collections Summary - Concatenation

All collections also have a common syntax for concatenating elements.

  • Arrays, Lists Maps and Sets can be concatenated using the ++ method
  • You can chain this to concatenate multiple collections, e.g. List(1, 2, 3) ++ List(4, 5, 6) ++ List(7, 8, 9)
  • For Maps duplicate keys are overwritten (in the result) by the rightmost Map that has them, e.g. Map("key" -> "value") ++ Map("key" -> "value2") will generate a new Map containing Map("key" -> "value2")
  • Concatenation is not modifying the collections, it simply generates a new array that contains all elements of the first list followed by the elements of the second and so forth

See Also opens in new page

Code editor is using Scalakata.com written by Guillaume Massé