How to get from an Iterator like this
val it = Iterator("one","two","three","four","five")
a map like
Map(four -> 4, three -> 5, two -> 3, five -> 4, one -> 3)
var m = Map[String, Int]() while (it.hasNext) { val cell = it.next() m += (cell -> cell.length()) } this is a solution using var but I'd like to use just Immutable and val variable.
If I use the for yield statement the returning object would be a Iterator[Map] and I do not want that:
val m = for(i<- it if it.hasNext) yield Map(i->i.length())
forversion is very odd. You don't need theif- theforalready handles that, and youyielda separateMapfor each entry, instead ot just the tuple.{for (i <- it) yield (i -> i.length)}.toMapwould do it.