确保使用 Scala HashMap 而不是 java HashMap。你确定你没有看错错误信息吗?
scala> import java.util.HashMap
import java.util.HashMap
scala> def foo (opthash : HashMap[Int,String]) = {
| val int_strin_list = opthash.toSeq.sortBy(_._1);
| "return something"
| }
<console>:13: error: value toSeq is not a member of java.util.HashMap[Int,String]
val int_strin_list = opthash.toSeq.sortBy(_._1);
^
正确的做法是:
scala> import scala.collection.immutable.HashMap
import scala.collection.immutable.HashMap
scala> def foo (opthash : HashMap[Int,String]) = {
| val int_strin_list = opthash.toSeq.sortBy(_._1);
| "return something"
| }
foo: (opthash: scala.collection.immutable.HashMap[Int,String])String
或者如果是这种情况,也可以使用可变的 HashMap。