这是我从 Java 转换的部分代码
class Regex(valCollection: Collection[MyClass3]) {
private val val1 = new HashMap[String, MyClass1]
private val val2 = new HashMap[String, String]
private val val3 = new HashMap[String, MyClass2]
private val filteredValCollection = valCollection.map(x => {
val valCollectionItem = getValCollectionItem(x)
x.key match {
case "123" => val1 + (valCollectionItem -> MyClass1.parse(x.value)) //TODO -- append to val1
case "456" => val2 + (valCollectionItem -> x.value) //TODO -- append to val2
case "789" => val3 + (valCollectionItem -> MyClass2.parse(x.value)) //TODO -- append to val3
}
valCollectionItem
})
def getValCollectionItem = { /*.....*/}
}
1)我想要做的是只使用不可变集合和不变性初始化所有4个集合:val1, val2, val3 and filteredValCollection
. 如您所见,filteredValCollection
已初始化,这很好。然而,
case "123" => val1 + (valCollectionItem -> MyClass1.parse(x.value))
将结果返回到无处以及 forval2
和val3
。
我的想法是,我必须返回一个tuple
fromvalCollection.map
并在之后初始化我想要的所有集合。
那么我该怎么做呢?
2)由于这段代码来自Java代码,Collection[MyClass3]
在Scala世界中是否有更有效的模拟?