Scala List
FAQ: How do I add elements to a Scala List
?
Solution
"How do I add elements to a Scala List
” is actually a bit of a trick question, because you can't add elements to a Scala List; it's an immutable data structure. If you’ve ever used the Java String
type, it’s just like that, you can’t mutate its elements.
That being said, in the following sections I’ll show what you can do.
Prepending elements to Scala Lists
The most common way to “add” elements to a Scala List
is to create a new List
from an existing List
by prepending elements to the existing list. We do this all the time in functional programming in Scala, and the general approach looks like this in the Scala REPL:
scala> val p1 = List("Kim") p1: List[String] = List(Kim) scala> val p2 = "Julia" :: p1 p2: List[String] = List(Julia, Kim) scala> val p3 = "Judi" :: p2 p3: List[String] = List(Judi, Julia, Kim)
These examples show how to start with the list p1
, and then create p2
by prepending one element to p1
. Then I prepend another element to p2
to create p3
. Prepending elements like this is exactly how the Scala List
class is intended to be used.
While that approach looks cumbersome in a small example, it makes sense in larger, real-world code, where we often do this inside a for
-expression. You can see more/better examples of this approach in my tutorial, Scala List class examples.
Use a ListBuffer when you want a “List” you can modify
If you want to use a Scala sequence that has many characteristics of a List
and is also mutable — i.e., you can add and remove elements in it — the correct approach is to use the Scala ListBuffer class instead, like this:
import scala.collection.mutable.ListBuffer var fruits = new ListBuffer[String]() fruits += "Apple" fruits += "Banana" fruits += "Orange"
Then convert it to a List
if/when you need to:
val fruitsList = fruits.toList
ListBuffer example in the Scala REPL
Here's what this List
and ListBuffer
example looks like using the Scala command line (REPL):
scala> import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer scala> var fruits = new ListBuffer[String]() fruits: scala.collection.mutable.ListBuffer[String] = ListBuffer() scala> fruits += "Apple" res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple) scala> fruits += "Banana" res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana) scala> fruits += "Orange" res2: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana, Orange) scala> val fruitsList = fruits.toList fruitsList: List[String] = List(Apple, Banana, Orange)
Note: Depending on your needs, it may be better to use an ArrayBuffer
rather than a ListBuffer
. See my Scala Cookbook Make the ArrayBuffer Your Default Mutable, Indexed Sequence tutorial for more information.
More functional ways to work with Scala lists
Depending on your needs, there are other, "more functional" ways to work with Scala lists, and I work through some of those in my Scala List examples. But for my needs today, I just wanted to work with a Scala List
like I'd work with a Java List (ArrayList
, LinkedList
), and this approach suits me.
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |