2016-12-15 5 views
0

いくつかの実験でSparks AccumulableParam[mutable.HashMap[Int,Long], Int]をScalaに拡張しています。これの一部は、メソッドdef addInPlace(t1: mutable.HashMap[Int,Long], t2: mutable.HashMap[Int,Long]): mutable.HashMap[Int,Long]を定義することです。でも、両方の用語t1ものの、++オペレータが代わりにHashMapの地図を返します。この場合、mutable.Mapをmutableに変換する方法。ScalaのHashMap?

Expression of type mutable.Map[Int, Long] doesn't conform to selected type mutable.HashMap[Int, Long]

import scala.collection.mutable.HashMap 

def addInPlace(t1: mutable.HashMap[Int,Long], t2: mutable.HashMap[Int,Long]): 
mutable.HashMap[Int,Long] = { 
    t1 ++ t2.map { case (s, c) => (s, c + t1.getOrElse(s, 0L)) } 
} 

が、私はエラーを取得する:私は何をしたいか

t2.map {...}は、タイプHashMap[int, Long]です。

だから私の質問は++作る代わりに、HashMapを返す、またはHashMapに結果Mapを変換する方法する方法、です。

+1

?カバーの下にある 'Map'は' HashMap'を使います。 –

+0

彼が言ったことは^^^。すべてを 'Map'と宣言してください。 – Dima

答えて

0

それをする醜い方法は、結果マップ上asInstanceOf[mutable.HashMap[Int, Long]]を使用することです:あなたは具体的な `HashMap`で作業する必要があるのはなぜ

def addInPlace(t1: mutable.HashMap[Int,Long], t2: mutable.HashMap[Int,Long]): mutable.HashMap[Int,Long] = { 
    val result = t1 ++ t2.map { case (s, c) => (s, c + t1.getOrElse(s, 0L)) } 
    result.asInstanceOf[mutable.HashMap[Int, Long]] 
} 
関連する問題