Reputation: 661
I have a Map in Scala with two keys:-
scala> Map(("Alf", "111-111-111") -> 1)
res0: scala.collection.immutable.Map[(String, String),Int] = Map((Alf,111-111-111) -> 1)
I want to flatten this to create a List as below:-
List(Alf, 111-111-111, 1)
This seems a simple task, so I apologise in advance if the answer is obvious.
Upvotes: 0
Views: 647
Reputation: 74
This also seems to work:
Map(("Alf", "111-111-111") -> 1).flatMap(x => List(x._1._1, x._1._2, x._2))
Upvotes: 0
Reputation: 3078
Something like this?
Map(("Alf", "111-111-111") -> 1).flatMap { case ((fst, snd), value) => List(fst, snd, value) }
Upvotes: 2
Reputation: 4779
Map(("Alf", "111-111-111") -> 1).toList.flatMap(tup => tup._1.productIterator.toList :+ tup._2)
This would result in
List[Any] = List(Alf, 111-111-111, 1)
I am not sure that that is what you actually want since you say you have a map with two keys and you don't. You have a map with one key which is a tuple of arity 2
Upvotes: 0