Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created October 29, 2020 00:54
Show Gist options
  • Save rupeshtr78/7c51886105e57a55c01db3f3c28ae3c3 to your computer and use it in GitHub Desktop.
Save rupeshtr78/7c51886105e57a55c01db3f3c28ae3c3 to your computer and use it in GitHub Desktop.
scala
Immutable
val numList = List[Int](1, 2, 3)
val numList2 = List[Int](10, 20, 30)
val numList3 = 1 :: 2:: 3:: Nil //List(1, 2, 3)
val numlist3 = numList ++ numList2 //List(1, 2, 3, 10, 20, 30)
val x = 100 :: numList //List(100, 1, 2, 3)
val x1 = numList :+ 100 //List(1, 2, 3, 100)
val x2 = 100 +: numList //List(100, 1, 2, 3)
val x3 = numList :: numList2 //List(List(1, 2, 3), 10, 20, 30)
val y = numList +: numList2 //List(List(1, 2, 3), 10, 20, 30)
val x = numList +: numList2 :+ 1000 //List(List(1, 2, 3), 10, 20, 30, 1000)
val ap = 42 +: numList :+ 89 //List(42, 1, 2, 3, 89)
Mutable
val b1 = Buffer(1,2,3)
val b2= Buffer(100,200,300)
val b3 = b1 ++= b2 //Vector(1, 2, 3, 100, 200, 300)
val b4 = b1 ++= numList2 //ArrayBuffer(1, 2, 3, 100, 200, 300, 10, 20, 30)
val b4 = b1 --= numList2 //ArrayBuffer(1, 2, 3, 100, 200, 300)
val b4 = b1 -= 100 //ArrayBuffer(1, 2, 3, 200, 300)
val b4 = b1 += 100 //ArrayBuffer(1, 2, 3, 200, 300, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment