Wednesday, April 24, 2019

Scala Collections

Useful operations on scala collections:

scala> Vector(1,2,3)
res0: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

scala> Seq(1,2,3)
res1: Seq[Int] = List(1, 2, 3)

scala> Set(1,2,3)
res2: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> Set(1,2,"3")
res3: scala.collection.immutable.Set[Any] = Set(1, 2, 3)

Collections has a default apply method

scala> Vector(1,2,3)
res4: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

scala> Vector.apply(1,2,3)
res5: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

Type of collection can be passed as parameter in []

scala> Vector[Int](1,2,3)
res6: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

scala> Vector[String]("a","b","c")
res8: scala.collection.immutable.Vector[String] = Vector(a, b, c)

scala> Vector("a","b","c")
res9: scala.collection.immutable.Vector[String] = Vector(a, b, c)

                 
Collection operations like +, toSet, toSeq, contains, head, last, tail, 

scala> val a = Seq(1,2)
a: Seq[Int] = List(1, 2)

scala> val b = Seq(3,4)
b: Seq[Int] = List(3, 4)

scala> a.toSet
res16: scala.collection.immutable.Set[Int] = Set(1, 2)

scala> Map((1,"a"),(2,"b"))
res17: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

scala> val map = Map((1,"a"),(2,"b"))
map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

scala> map.toSet
res18: scala.collection.immutable.Set[(Int, String)] = Set((1,a), (2,b))

scala> map.toSeq
res20: Seq[(Int, String)] = ArrayBuffer((1,a), (2,b))

scala> a.contains(1)
res21: Boolean = true

scala> a.head
res22: Int = 1

scala> a.tail
res23: Seq[Int] = List(2)

Sequence

scala> val numbers = (1,2,3,4)
numbers: (Int, Int, Int, Int) = (1,2,3,4)

scala> val numbers = Seq(1,2,3,4)
numbers: Seq[Int] = List(1, 2, 3, 4)

scala> numbers(0)
res26: Int = 1

scala> numbers:+4
res27: Seq[Int] = List(1, 2, 3, 4, 4)

scala> 0+:numbers
res28: Seq[Int] = List(0, 1, 2, 3, 4)

scala> Seq(1,2,1,3).distinct
res30: Seq[Int] = List(1, 2, 3)

scala> numbers
res31: Seq[Int] = List(1, 2, 3, 4)

scala> val numers = numbers:+4
numers: Seq[Int] = List(1, 2, 3, 4, 4)

scala> numers.distinct
res33: Seq[Int] = List(1, 2, 3, 4)

scala> val numers0 = 0+:numbers:+6
numers0: Seq[Int] = List(0, 1, 2, 3, 4, 6)

scala> val numers0 = 0+:numbers:+6+7
numers0: Seq[Int] = List(0, 1, 2, 3, 4, 13)

scala> val numers0 = 0+:numbers:+6:+7
numers0: Seq[Int] = List(0, 1, 2, 3, 4, 6, 7)


scala> val numers0 = 0+:numbers:+6:+7
numers0: Seq[Int] = List(0, 1, 2, 3, 4, 6, 7)

scala> numbers.+:(0)
res34: Seq[Int] = List(0, 1, 2, 3, 4)


Set has no duplicates

scala> val numbers = Set(1,2,3)
numbers: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> numbers+7
res35: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 7)

scala> numbers(1)
res38: Boolean = true

scala> numbers contains 1
res40: Boolean = true


Maps
Collection of key value pairs with unique keys

scala> val map = Map( 1->"a", 2->"b" )
map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

scala> map(1)
res47: String = a

scala> map.get(9)
res48: Option[String] = None

scala> map.getOrElse(1,"z")
res49: String = a

scala> map.getOrElse(9,"z")
res50: String = z


Scala collections come in 3 flavors (default immutable is used - without imports)
- scala.collection package (base)
- scala.collection.immutable 
- scala.collection.mutable

Exception - scala.collection.Seq (because of Arrays)
Immutable collections are persistent

No comments:

Post a Comment