Here is a part of code I converted from 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) What I want to do is using only immutable collections and immutability initialize all 4 collections: val1, val2, val3 and filteredValCollection
. As you can see, filteredValCollection
was initialized and that's fine. However,
case "123" => val1 + (valCollectionItem -> MyClass1.parse(x.value))
returns a result to nowhere as well as for val2
and val3
.
My thought is, I have to return a tuple
from valCollection.map
and initialize all the collections I want afterwards.
So how do I do that?
2) Since this code came from Java code, is there any more efficient analog of Collection[MyClass3]
in Scala world?