2. AgendaAgenda
● Why Collections? I love collections
● Introduction to Scala collections
● Code examples
● Why Collections? I love collections
● Introduction to Scala collections
● Code examples
3. Why collections?Why collections?
No complicated looping structures and recursions
Side-effect-free operations
No interference between iterators and collections
No complicated looping structures and recursions
Side-effect-free operations
No interference between iterators and collections
7. Lets begin the collection
Lists
scala> val Numbers = List(1, 2, 3, 4)
numbers: List[Int] = List(1, 2, 3, 4)
Numbers :+= 5 //error of reassignment
Numbers ++= List(6,7,8) //error of reassignment
Numbers ++= List(9,”Harsh”)//type mismatch
8. Lets begin the collection
Sets
Sets have no duplicates
scala> var Numbers = Set(1, 1, 2)
res0: scala.collection.immutable.Set[Int] = Set(1, 2)
Numbers += “Knoldus” // type mismatch error
Numbers += 2 // blank or no output having 1 and 2 as elements
Numbers += 3 //scala.collection.immutable.Set[Int] =
Set(1, 2, 3)
9. Lets begin the collection
Tuple
A tuple groups together simple logical collections of
items without using a class.
scala> val hostPort = ("localhost", 80)
hostPort: (String, Int) = (localhost, 80)
10. Lets begin the collection
Tuples do not have named accessors, like case classes
Just have their position as 1-based
Have limit of 22 elements
scala> hostPort._1
res0: String = localhost
scala> hostPort._2
res1: Int = 80
11. Lets begin the collection
Tuples satisfies the pattern matching nicely
hostPort match {
case ("localhost", port) => ...
case (host, port) => ...
}
12. Lets begin the collection
Special sauce to create tuple of two elements
scala> 1 -> 2
res0: (Int, Int) = (1,2)
scala> "Harsh"->"Sharma"->"Khandal"
res4: ((String, String), String) = ((Harsh,Sharma),Khandal)
13. Lets begin the collection
● Map can hold basic datatypes
● Collection of tuples
● Can contain maps and functions as values
● Map(1 -> 2)
Map("foo" -> "bar")
● Map(1 -> Map("foo" -> "bar"))
● Map("timesTwo" -> { timesTwo(_) })
14. Lets begin the collection
Option[T] is used when the method might not return what we ask
for
● scala> val numbers = Map("one" -> 1, "two" -> 2)
numbers: scala.collection.immutable.Map[java.lang.String,Int] =
Map(one -> 1, two -> 2)
● scala> numbers.get("two")
res0: Option[Int] = Some(2)
● scala> numbers.get("three")
res1: Option[Int] = None
15. Functional combinators
map: evaluates a function over each element of list
scala> val numbers=List(1,2,3,4)
numbers: List[Int] = List(1, 2, 3, 4)
scala> numbers.map((i: Int) => i * 2)
res1: List[Int] = List(2, 4, 6, 8)
foreach: A kind of map but returns nothing
scala> val doubled = numbers.foreach((i: Int) => i * 2)
doubled: Unit = ()
16. Functional combinators
filter: removes elements when function we pass evaluates to
false
scala> numbers.filter((i: Int) => i % 2 == 0)
res0: List[Int] = List(2, 4)
zip: Zips contents of multiple lists into one single list
scala> List(1, 2, 3).zip(List("a", "b", "c"))
res0: List[(Int, String)] = List((1,a), (2,b), (3,c))
17. Functional combinators
partition: splits a list based on where it falls with respect to a
predicate function.
scala> val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> numbers.partition(_ % 2 == 0)
res0: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))
find: returns the first element of a collection that matches a
function.
scala> numbers.find((i: Int) => i > 5)
res0: Option[Int] = Some(6)
18. Functional combinators
drop: drops the first i elements
scala> numbers.drop(5)
res0: List[Int] = List(6, 7, 8, 9, 10)
● dropWhile: removes the first elements that match a predicate
function
scala> numbers.dropWhile(_ % 2 != 0)
res0: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10)
19. Functional combinators
foldLeft: value passed as argument is the initial value and m is
the accumulator
scala> numbers.foldLeft(0)((m: Int, n: Int) => m + n)
res0: Int = 55
– scala> numbers.foldLeft(0) { (m: Int, n: Int) => println("m: " + m + " n: " + n); m + n }