Combining a list of maps in scala -


i have following datastructure:

list(map(     1365094146000000 -> map(latitude -> 45.30397),      1365094752000000 -> map(latitude -> 45.30405),      1365094449000000 -> map(latitude -> 45.30412),      1365095351000000 -> map(latitude -> 45.30400),      1365095054000000 -> map(latitude -> 45.30400)),      map(     1365094146000000 -> map(longitude -> -75.89806),      1365094752000000 -> map(longitude -> -75.89806),      1365094449000000 -> map(longitude -> -75.89811),      1365095351000000 -> map(longitude -> -75.89804),      1365095054000000 -> map(longitude -> -75.89809))) 

whats best way merge these maps resulting object following:

map(1365094146000000 -> map(latitude -> 45.30397, longitude -> -75.89806),     1365094752000000 -> map(latitude -> 45.30405, longitude -> -75.89806)) 

thanks

as input structure, latitude , longtitude should strings. also, timestamps should longs they're out of int's range. instance,

val lst = list(map(     1365094146000000l -> map("latitude" -> 45.30397),      1365094752000000l -> map("latitude" -> 45.30405),      1365094449000000l -> map("latitude" -> 45.30412),      1365095351000000l -> map("latitude" -> 45.30400),      1365095054000000l -> map("latitude" -> 45.30400)),      map(     1365094146000000l -> map("longitude" -> -75.89806),      1365094752000000l -> map("longitude" -> -75.89806),      1365094449000000l -> map("longitude" -> -75.89811),      1365095351000000l -> map("longitude" -> -75.89804),      1365095054000000l -> map("longitude" -> -75.89809))) 

once that's fixed, can do:

yourlist.flatten.groupby(_._1) map { case (key, value) => key -> value.map(_._2).flatten.tomap } tomap 

first, merges 2 maps together. then, groups entries timestamp form map[long,list[(long, map[string,double])]]. after that, we're @ solution , have rid of timestamp duplication in value (value.map(_._2)), flatten latitudes , longitudes, , convert them map. finally, convert output list map.

the expanded version types looks this:

yourlist.flatten.groupby { x: (long, map[string, double]) =>      x._1 } map { case (key: long, value: list[(long, map[string, double])]) =>          key -> value.map { x: (long, map[string, double]) =>              x._2          }.flatten.tomap      } tomap 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -