Introduction to Scala Part2 : Collections

Bubu
712 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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)
createList
should return a List create by ListUtil
should add e as first element of l
should add e as last element of l
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content