score:0

val m3 = (map1.toseq ++ map2.toseq). // combine the maps
   groupby (x=>x._1). //group by the original keys
   map{case (k,lst)=> (k, lst.map(x=> x._2))}. //strip the keys from the grouped sequences
   filter{case (_, lst) => lst.forall(i => lst.head.getclass == i.getclass)}. //filter out hetergeneous seqs
    tomap // make a map

score:0

without forall:

(map1.tolist ++ map2.tolist).groupby(_._1).mapvalues(_.map(_._2))
  .filter(_._2.map(_.getclass).toset.tail.isempty)

map(third -> list(orange(), orange()), second -> list(banana(3), banana(4)))

this version requires a little more (but still linear inside filter) cpu and memory than version with forall, so you should use it only for small collections.

score:1

scala> val r: map[string, seq[fruit]] = (map1.tolist ++ map2.tolist).
   groupby(x => x._1).
   mapvalues(lst => lst.map(x => x._2)).
   .filter { 
     case (key, lst) => lst.forall(x =>
             x.getclass == lst.head.getclass)
   }

r: map[string, seq[fruit]] = map(third -> list(orange(), orange()), 
    second -> list(banana(3), banana(4)))

Related Query

More Query from same tag