Collections · 24 of 42

Lists

  • Lists are constructed simply using List(element1, element2, ...)
  • List elements can be of any type, but the List final type will be the lowest common denominator

    class Foo(val value1:Int)
    class Bar(value1:Int, val value2:Int) extends Foo(value1)
    val list:List[Foo] = List(new Foo(1), new Bar(2,3))
    
  • The default List in Scala is scala.List, which points to scala.collection.immutable.List src docs, and defined in scala/package.scala src docs
  • The default List is implemented as a linked list
  • It is immutable (any “changes” create a new list, the original is untouched)
//Immutable list of type List[Int]
val list1 = List(1, 2, 3)
//Immutable list of type List[Any]
val list2 = List("a", 2, true)
import collection.mutable
//the "mutable version" of List
val mlist = mutable.ArrayBuffer("a", "b", "c")

//Access items using (index) not [index]
val firstItem = list1(0)

//Modify items the same way (mutable Lists only)
mlist(0) = "d"
mlist

//Concatenation using the ++ operator or ::: (lists only)
list1 ++ list2
list1 ::: list2

//Prepending an item using either :: (lists only) or +:
0 :: list1
0 +: list1

//appending an item using :+ (not efficient for immutable List)
list1 :+ 4

//all together
val concatenated = 1 :: list1 ::: list2 ++ mlist :+ 'd'
//concatenation doesn't modify the lists themselves
list1

//Removing elements with diff (creates a new collection):

//creates a new ArrayBuffer with "c" removed, mlist is not touched
mlist diff List("c")
//creates a new ArrayBuffer with e, f removed (they are not present), mlist is not touched
mlist diff List("e", "f")
//mlist not modified
mlist

//Removing elements in place (mutable Lists only):

//removes c from the list itself
mlist -= "c"
mlist
//removes e and f from mlist itself (no-op here, they are not present)
mlist --= List("e", "f")
mlist

//Adding elements (mutable Lists only)
mlist += "e"
mlist ++= List("f", "g")

mlist

//Diff
val diffList = List(1,2,3,4) diff List(2,3)

//Find (stops when item is found)
val personList = List(("Alice",1), ("Bob",2), ("Carol",3))
def findByName(name:String) = personList.find(_._1 == name).getOrElse(("David",4))
val findBob = findByName("Bob")
val findEli = findByName("Eli")

findBob._2
findEli._2

Contents

Scala basics

  1. Overview
  2. Scalculator
  3. Operators are methods
  4. Variables
  5. Final variables
  6. Printing values
  7. String interpolation
  8. String formatting
  9. Useful operations
  10. Method definition
  11. Method definition 2
  12. Method definition 3
  13. Anonymous functions
  14. Anonymous functions 2
  15. Return multiple values
  16. Declare multiple variables
  17. Assign multiple variables
  18. Loops using while
  19. Loops using for
  20. Loops without loops
  21. If
  22. Match as a switch
  23. Arrays
  24. Lists
  25. Sets
  26. Maps
  27. Mutable collections
  28. Collections: accessing elements
  29. Collections: concatenation
  30. Mutable collection operations
  31. Immutable collections with var
  32. Collections: useful methods
  33. Classes
  34. Classes, continued

What's new in Scala 3

  1. Scala 3: what changed
  2. Optional braces & new control syntax
  3. Top-level definitions & @main
  4. Enums & ADTs
  5. Extension methods
  6. given & using
  7. Union & intersection types
  8. Smaller niceties