0
请按照下面的代码。与斯卡拉混淆flatMap,地图和拼合
import scala.collection.immutable.HashMap
def myFunc(map : HashMap[Char,List[MyObject]], text : List[Char]) : List[MyObject] = {
text.flatMap(ch => map.get(ch)) //Gives compilation error
text.map(ch => map.get(ch)).flatten //gives compilation error
text.flatMap(ch => map.get(ch)).flatten //This works
}
我不明白为什么前两种方法不起作用?
编辑
我得到这个错误在第2行
Expression List[List[MyObject]] doesn't confirm to expected type list List[MyObject]
什么是错误?它是“纯粹的表达式在不确定的情况下使用”或类似的东西? – Carcigenicate
@Carcigenicate我编辑了错误的问题 – Ashwin
@pedromss我编辑了错误的问题 – Ashwin