List Initialize
This page was last reviewed on Oct 3, 2023.
Dot Net Perls
Initialize list. In Scala lists are immutable. This presents a problem when creating lists. We sometimes need complicated initialization logic.
Shows a list
Syntax. List.newBuilder can be used to add many values into a temporary collection and then convert that to a list. We can initialize lists by combining 2 lists or adding elements.
Builder example. Here we create a list with the List constructor syntax (a list literal). Then we use List.newBuilder to create the same list with a Builder.
Tip We need to specify the type of elements as part of the List.newBuilder function call. Here I use Int.
And We can add elements to the Builder's internal buffer by using the addition operator. With result() we get the final immutable list.
Shows a list
object Program { def main(args: Array[String]): Unit = { // Create a list with values. val numbers = List(10, 11, 12) println(numbers) // Create a list with a Builder. val builder = List.newBuilder[Int] builder += 10 builder += 11 builder += 12 val numbers3 = builder.result() println(numbers3) } }
List(10, 11, 12) List(10, 11, 12)
Combine two lists. Sometimes the easiest way to initialize a list is to combine two (or more than two) lists that already exist. Here we create two small lists and then merge them.
object Program { def main(args: Array[String]): Unit = { // Add two lists together. val numbers1 = List(13, 14) val numbers2 = List(15, 16) val numbers3 = numbers1 ::: numbers2 println(numbers3) } }
List(13, 14, 15, 16)
New element. Here we create a list by taking an existing list and adding another element to its start. This changes the head of the list. This only works with individual elements.
Warning If we use "::" with another list, that entire list (not its individual elements) will be placed in the head of the new list.
object Program { def main(args: Array[String]): Unit = { // Initialize a list by placing an element at its start. val numbers1 = List(100, 200) val numbers2 = 50 :: numbers1 println(numbers2) } }
List(50, 100, 200)
List.empty. This can be used to initialize lists. We can place new elements alongside List.empty and initialize a new list. We must specify the type of elements (like String).
object Program { def main(args: Array[String]): Unit = { // Initialize an empty list and add an element to it. val numbers1 = List.empty[String] val numbers2 = "cat" :: numbers1 println(numbers2) } }
A summary. Lists are immutable in Scala, so initializing lists is critical. With List.newBuilder and List.empty we can begin initializing lists with ease.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 3, 2023 (simplify).
© 2007-2024 Sam Allen.