【发布时间】:2018-09-22 16:20:05
【问题描述】:
正如下面的(不起作用的)代码所示,我想获取任何哈希图并转换为电子表格格式。
def printHashMapToCsv[T](m:Map[T,T],name:String, path:String): Unit =
{
Spreadsheet.initCsvSpreadsheet(name,path)
m.foreach
{
e=> Spreadsheet.addCell(IDGenerator.getInstance().nextID(),e._1.toString,e._2.toString)
}
Spreadsheet.printCsvFinal()
}
上面的代码可以编译,但是当我尝试使用下面的代码调用该方法时,我得到了一个编译方法:
def mapOut(): Unit =
{
try{
val m:mutable.HashMap[Int,String]=new mutable.HashMap[Int,String]()
m.put(1,"sssss")
m.put(2,"ghfd")
m.put(3,"dfsa")
m.put(4,"fhjgsdf")
printHashMapToCsv(m,"mapout",s"${new File(".").getAbsolutePath}${File.separator}unitTestOutput")
}
编译错误:
错误:类型不匹配; [信息] 发现: scala.collection.mutable.HashMap[Int,String] [INFO] 需要: 地图[?,?]
任何建议将不胜感激
【问题讨论】:
-
您的
printHashMapToCsv[T]方法需要Map[T, T](即相同类型的键和值),而您正在为其提供mutable.HashMap[Int, String]。 -
知道了,这解释了错误