Different ways to create and populate Lists in Scala

This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). This is Recipe 11.1, “Different Ways to Create and Populate a List in Scala”

Problem

You want to create and populate a Scala List.

Solution: Ways to populate a Scala List

There are many ways to create and initially populate a List:

// 1
scala> val list = 1 :: 2 :: 3 :: Nil
list: List[Int] = List(1, 2, 3)

// 2
scala> val list = List(1, 2, 3)
x: List[Int] = List(1, 2, 3)

// 3a
scala> val x = List(1, 2.0, 33D, 4000L)
x: List[Double] = List(1.0, 2.0, 33.0, 4000.0)

// 3b
scala> val x = List[Number](1, 2.0, 33D, 4000L)
x: List[java.lang.Number] = List(1, 2.0, 33.0, 4000)

// 4 (ranges)
scala> val x = List.range(1, 10)
x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> val x = List.range(0, 10, 2)
x: List[Int] = List(0, 2, 4, 6, 8)

scala> val x = (1 to 3).toList
x: List[Int] = List(1, 2, 3)

// 5
scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

// 6
scala> val x = List.tabulate(5)(n => n * n)
x: List[Int] = List(0, 1, 4, 9, 16)

// 7
scala> val x = collection.mutable.ListBuffer(1, 2, 3).toList
x: List[Int] = List(1, 2, 3)

// 8
scala> "foo".toList
res0: List[Char] = List(f, o, o)

The first two approaches shown are the most common and straightforward ways to create a List. Examples 3a and 3b show how you can manually control the List type when your collection has mixed types. When the type isn’t manually set in Example 3a, it ends up as a List[Double], and in 3b it’s manually set to be a List[Number].

Examples 4 through 6 show different ways to create and populate a List with data.

Examples 7 and 8 show that many collection types also have a toList method that converts their data to a List.

Discussion

Going back to the first example, it shows the :: method for creating a List, which will be new to Java developers. As shown, the :: method (called “cons”) takes two arguments: a head element, which is a single element, and a tail, which is another List. When a List is constructed like this, it must end with a Nil element.

It’s important to know that the Scala List class is not like Java List classes, such as the Java ArrayList. For example, Recipe 17.1, “Going to and from Java Collections” shows that a java.util.List converts to a Scala Buffer or Seq, not a Scala List.

The following quote from the Scala List scaladoc discusses the important properties of the List class:

“This class is optimal for last-in-first-out (LIFO), stack-like access patterns. If you need another access pattern, for example, random access or FIFO, consider using a collection more suited to this than List. List has O(1) prepend and head/tail access. Most other operations are O(n) on the number of elements in the list.”

See Recipe 10.4, “Understanding the Performance of Collections” for more information on the List performance characteristics.

See Also

  • The List class
  • Recipe 3.15, “Working with a List in a Match Expression” shows how to handle a List in a match expression, especially the Nil element
  • Recipe 10.4, “Understanding the Performance of Collections” discusses List class performance
  • Recipe 17.1, “Going to and from Java Collections” demonstrates how to convert back and forth between Scala and Java collections