Back
Close

Introduction to Scala Part2 : Collections

Bubu
5,569 views

List

List creation

  • List.apply
> List.apply(1, 2, 3, 4)
res53: List[Int] = List(1, 2, 3, 4)
  • List.::
> 1 :: 2 :: 3 :: 4 :: Nil
res6: List[Int] = List(1, 2, 3, 4)
  • Nil
object Nil extends List[Nothing] with Product with Serializable

List

Some method of List

  • List.range
List.range(1, 5)
res54: List[Int] = List(1, 2, 3, 4)
  • List.fill
List.fill(5)('a')
res57: List[Char] = List(a, a, a, a, a)
List.fill(5)('a')
res57: List[Char] = List(a, a, a, a, a)
  • List.concat
> List.concat(List('a', 'b'), List('c'))
res60: List[Char] = List(a, b, c)

Lists - Complexity

  • Insert at front: o(1)
  • Insert at end: o(n)
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io