Advertisement
jmvanel

generate a map from list

Jan 15th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.54 KB | None | 0 0
  1. import scala.util.Random
  2.  
  3. /**  my use case is:
  4.  *   I have a List[Item];  Item has a field id that is sometimes
  5.  *   not defined == "" ; I want to generate a complete map with id as a key
  6.  *   when defined or else a new key . */
  7. object UpdateKey extends App {
  8.   case class Item(val id: String)
  9.   val list = List(Item("bla"), Item(""))
  10.   val listOfPairs = list.map {
  11.     item =>
  12.       {
  13.         if (item.id != "")
  14.           (item.id, item)
  15.         else
  16.           (Random.nextInt(), item)
  17.       }
  18.   }
  19.   println(Map(listOfPairs: _*))
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement