I have a Seq
containing objects of a class that looks like this:
class A (val key: Int, ...)
Now I want to convert this Seq<
As scala knows to convert a Tuple of two to a map, you would first want to convert your seq to a tuple and then to map so (doesn't matter if it's int, in our case string, string):
The general algorithm is this:
Or to sum up:
Step 1: Seq --> Tuple of two
Step 2: Tuple of two --> Map
Example:
case class MyData(key: String, value: String) // One item in seq to be converted to a map entry.
// Our sequence, simply a seq of MyData
val myDataSeq = Seq(MyData("key1", "value1"), MyData("key2", "value2"), MyData("key3", "value3")) // List((key1,value1), (key2,value2), (key3,value3))
// Step 1: Convert seq to tuple
val myDataSeqAsTuple = myDataSeq.map(myData => (myData.key, myData.value)) // List((key1,value1), (key2,value2), (key3,value3))
// Step 2: Convert tuple of two to map.
val myDataFromTupleToMap = myDataSeqAsTuple.toMap // Map(key1 -> value1, key2 -> value2, key3 -> value3)