Convert java.util.Map to Scala List[NewObject] -
i have java.util.map[string, myobject] , want create scala list[mynewobject] consisting of alle entries of map special values.
i found way but, well, ugly:
val result = listbuffer[mynewobject]() myjavautilmap.foreach ( (es: entry[string, myobject]) => { result += mynewobject(es.getkey(), ey.getvalue().getmyparameter); println("aa")} )
how can rid of println("aa")? deleting not because foreach needs consumer += operation yields list....
is there more elegant way convert java.util.map list[mynewobject]?
scala has conversions give nice methods of scala collection api on java collections:
import collection.javaconversions._ val result = myjavautilmap.map{ case (k,v) => mynewobject(k, v.getmyparameter) }.tolist
by way: define function returns unit, can explicitly specify return type:
val f = (x: int) => x: unit
Comments
Post a Comment