Scala:	
  collec)ons	
  
The	
  collec)ons	
  API	
  
•  Very	
  rich	
  library	
  for	
  manipula)ng	
  collec)ons	
  
of	
  objects	
  
•  Great	
  examples	
  of	
  higher	
  order	
  func)ons	
  
•  The	
  usual	
  suspects:	
  Lists,	
  Sets,	
  Hash	
  Maps.	
  
•  But	
  also:	
  Streams,	
  Buffers,	
  Arrays,	
  Trees	
  
Let's	
  take	
  this	
  model	
  
case class Category(
name: String, 
parent: Option[Category])

case class Product(
name: String, 
description: String, 
category: Category)
Given	
  a	
  list	
  of	
  products	
  
•  Sort	
  them	
  by	
  name	
  
•  Get	
  their	
  names	
  in	
  uppercase	
  
•  Get	
  the	
  set	
  of	
  categories	
  (no	
  repeats)	
  
•  Group	
  them	
  by	
  the	
  ini)al	
  of	
  their	
  names	
  
•  Return	
  a	
  tuple	
  with	
  the	
  product	
  and	
  category	
  
names	
  
Sor)ng	
  
case class Product(
name: String, 
description: String, 
category: Category)

val products = List( ... ) // List of products

// Sort by name
products.sortBy(_.name)
Mapping	
  

// Get their names in uppercase
val names: List[String] = 

products.map(_.name.toUpperCase)
Extrac)ng	
  

// Get category name set
val catNames : Set[String] =
products.map(_.category.name).toSet
Grouping	
  

// Group by the initial of their names
val grouped : Map[Char, List[Product]] = 

products.groupBy(_.name.head)
More	
  Mapping	
  

// Create tuples of product and category
names

val pairs : Tuple2[String,String] = 
for {

product <- products

category = product.category 

} yield (product.name, category.name)
Scala: Collections API

Scala: Collections API

  • 1.
  • 2.
    The  collec)ons  API   •  Very  rich  library  for  manipula)ng  collec)ons   of  objects   •  Great  examples  of  higher  order  func)ons   •  The  usual  suspects:  Lists,  Sets,  Hash  Maps.   •  But  also:  Streams,  Buffers,  Arrays,  Trees  
  • 3.
    Let's  take  this  model   case class Category( name: String, parent: Option[Category]) case class Product( name: String, description: String, category: Category)
  • 4.
    Given  a  list  of  products   •  Sort  them  by  name   •  Get  their  names  in  uppercase   •  Get  the  set  of  categories  (no  repeats)   •  Group  them  by  the  ini)al  of  their  names   •  Return  a  tuple  with  the  product  and  category   names  
  • 5.
    Sor)ng   case classProduct( name: String, description: String, category: Category) val products = List( ... ) // List of products // Sort by name products.sortBy(_.name)
  • 6.
    Mapping   // Gettheir names in uppercase val names: List[String] = products.map(_.name.toUpperCase)
  • 7.
    Extrac)ng   // Getcategory name set val catNames : Set[String] = products.map(_.category.name).toSet
  • 8.
    Grouping   // Groupby the initial of their names val grouped : Map[Char, List[Product]] = products.groupBy(_.name.head)
  • 9.
    More  Mapping   //Create tuples of product and category names val pairs : Tuple2[String,String] = for { product <- products category = product.category } yield (product.name, category.name)