scala convert List of map to map - scala

I have a simple question
I have List of Map like this
List(
Map("a" -> "a"),
Map("b" -> "b")
)
And I want the result like this
Map(
"a"->"a",
"b"->"b"
)
It can be overwrite if the key is duplication
Any one please help me

You can combine flatten and toMap:
val list = List(Map("k1" -> "v1", "k2" -> "v2"))
list.flatten.toMap // Map(k1 -> v1, k2 -> v2)
flatten will convert the list of maps into a list of tuples and then toMap will convert your list of tuples into a map.

You can try using reduce:
scala> val list = List(Map("k1" -> "v1", "k2" -> "v2"))
list: List[scala.collection.immutable.Map[String,String]] = List(Map(k1 -> v1, k2 -> v2))
scala> list.reduce(_ ++ _)
res0: scala.collection.immutable.Map[String,String] = Map(k1 -> v1, k2 -> v2)
scala> val list = List(Map("k1" -> "v1"), Map("k2" -> "v2"))
list: List[scala.collection.immutable.Map[String,String]] = List(Map(k1 -> v1), Map(k2 -> v2))
scala> list.reduce(_ ++ _)
res1: scala.collection.immutable.Map[String,String] = Map(k1 -> v1, k2 -> v2)
This way you needn't convert to any intermediate data type.

Related

Invert a Map (String -> List) in Scala

I have a Map[String, List[String]] and I want to invert it. For example, if I have something like
"1" -> List("a","b","c")
"2" -> List("a","j","k")
"3" -> List("a","c")
The result should be
"a" -> List("1","2","3")
"b" -> List("1")
"c" -> List("1","3")
"j" -> List("2")
"k" -> List("2")
I've tried this:
m.map(_.swap)
But it returns a Map[List[String], String]:
List("a","b","c") -> "1"
List("a","j","k") -> "2"
List("a","c") -> "3"
Map inversion is a little more complicated.
val m = Map("1" -> List("a","b","c")
,"2" -> List("a","j","k")
,"3" -> List("a","c"))
m flatten {case(k, vs) => vs.map((_, k))} groupBy (_._1) mapValues {_.map(_._2)}
//res0: Map[String,Iterable[String]] = Map(j -> List(2), a -> List(1, 2, 3), b -> List(1), c -> List(1, 3), k -> List(2))
Flatten the Map into a collection of tuples. groupBy will create a new Map with the old values as the new keys. Then un-tuple the values by removing the key (previously value) elements.
An alternative that does not rely on strange implicit arguments of flatten, as requested by yishaiz:
val m = Map(
"1" -> List("a","b","c"),
"2" -> List("a","j","k"),
"3" -> List("a","c"),
)
val res = (for ((digit, chars) <- m.toList; c <- chars) yield (c, digit))
.groupBy(_._1) // group by characters
.mapValues(_.unzip._2) // drop redundant digits from lists
res foreach println
gives:
(j,List(2))
(a,List(1, 2, 3))
(b,List(1))
(c,List(1, 3))
(k,List(2))
A simple nested for-comprehension may be used to invert the map in such a way that each value in the List of values are keys in the inverted map with respective keys as their values
implicit class MapInverter[T] (map: Map[T, List[T]]) {
def invert: Map[T, T] = {
val result = collection.mutable.Map.empty[T, T]
for ((key, values) <- map) {
for (v <- values) {
result += (v -> key)
}
}
result.toMap
}
Usage:
Map(10 -> List(3, 2), 20 -> List(16, 17, 18, 19)).invert

Scala Map update

I want to update Map value which is present in another Map. When I try to update is says 'value update is not a member of Option[scala.collection.immutable.Map[Int,Int]]'.
I tried to convert the value to Map but still, it didn't work for me.
val map = Map("one" -> Map(1 -> 11), "two" -> Map(2 -> 22))
val value = map1.get("one")
value(1) = 100 //value update is not a member of Option[scala.collection.Map[Int,Int]]
There are two mistakes you are making.
Calling get on a Map will return an Option, hence you are not able to set the value.
You are using immutable Map when your operation/purpose is to update the value of some key, for which you need to use mutable map.
Let us try to do the write some snippets to solve these two problems.
scala> val map = Map("one" -> Map(1 -> 11), "two" -> Map(2 -> 22))
map: scala.collection.immutable.Map[String,scala.collection.immutable.Map[Int,Int]] = Map(one -> Map(1 -> 11), two -> Map(2 -> 22))
scala> val valueOption = map.get("one")
valueOption: Option[scala.collection.immutable.Map[Int,Int]] = Some(Map(1 -> 11))
scala> val value = map("one")
value: scala.collection.immutable.Map[Int,Int] = Map(1 -> 11)
scala> value(1) = 100
<console>:13: error: value update is not a member of scala.collection.immutable.Map[Int,Int]
value(1) = 100
You should notice the difference between getting the value using .get and directly using parenthesis. This is a more understandable error and no need to understand Scala magic happening underneath.
Now if you repeat the same statements after importing mutable Map, you will be able to get what you are trying to achieve.
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val map = Map("one" -> Map(1 -> 11), "two" -> Map(2 -> 22))
map: scala.collection.mutable.Map[String,scala.collection.mutable.Map[Int,Int]] = Map(one -> Map(1 -> 11), two -> Map(2 -> 22))
scala> val value = map("one")
value: scala.collection.mutable.Map[Int,Int] = Map(1 -> 11)
scala> value(1) = 100
scala> map
res2: scala.collection.mutable.Map[String,scala.collection.mutable.Map[Int,Int]] = Map(one -> Map(1 -> 100), two -> Map(2 -> 22))
When you created you first map it is already immutable which cannot be changed
scala> val map = Map("one" -> Map(1 -> 11), "two" -> Map(2 -> 22))
map: scala.collection.immutable.Map[String,scala.collection.immutable.Map[Int,Int]] = Map(one -> Map(1 -> 11), two -> Map(2 -> 22))
Your second command is returning an Option of immutable Map again and that can not be updated too.
scala> val value = map.get("one")
value: Option[scala.collection.immutable.Map[Int,Int]] = Some(Map(1 -> 11))
As chunjef suggested, you should be using mutable Map
scala> val map = Map("one" -> scala.collection.mutable.Map(1 -> 11), "two" -> scala.collection.mutable.Map(2 -> 22))
map: scala.collection.immutable.Map[String,scala.collection.mutable.Map[Int,Int]] = Map(one -> Map(1 -> 11), two -> Map(2 -> 22))

Scala two map merge

How can I merge maps like below:
val map1 = Map(1 -> "a", 2 -> "b")
val map2 = Map("a" -> "A", "b" -> "B")
After merged.
Merged = Map( 1 -> List("a", "A"), 2 -> List("b", "B"))
Can be List, Set or any other collection who has size attribute.
I'm not sure I understand what are you searching for exactly, but to achieve that for the provided example you could do:
val map1 = Map(1 -> "a", 2 -> "b")
val map2 = Map("a" -> "A", "b" -> "B")
map1.mapValues(value => (value, map2(value)))
However you should be careful to have every value from a as a key in b (I just assumed this happens from the provided example).
Given two maps with value1 as key2
scala> val x = Map(1 -> "a", 2 -> "b")
x: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)
scala> val y = Map("a" -> "A", "b" -> "B")
y: scala.collection.immutable.Map[String,String] = Map(a -> A, b -> B)
Merge as Map(k1 -> List(v1, v2))
scala> val z = x.map { case (k1, v1) => (k1, List(v1, y(v1))) }
z: scala.collection.immutable.Map[Int,List[String]] = Map(1 -> List(a, A), 2 -> List(b, B))
You basically need to get value from first map then lookup the second map, and just create a List out of those (v1, v2).
Try This
scala> val map1 = Map(1 -> "a", 2 -> "b")
map1: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)
scala> val map2 = Map("a" -> "A", "b" -> "B")
map2: scala.collection.immutable.Map[String,String] = Map(a -> A, b -> B)
scala> map1.zip(map2).map(x=>x._1._1 -> List(x._2._1,x._2._2))
res44: scala.collection.immutable.Map[Int,List[String]] = Map(1 -> List(a, A), 2 -> List(b, B))

Scala Map: Combine keys with the same value?

Suppose I have a Map like
val x = Map(1 -> List("a", "b"), 2 -> List("a"),
3 -> List("a", "b"), 4 -> List("a"),
5 -> List("c"))
How would I create from this a new Map where the keys are Lists of keys from x having the same value, e.g., how can I implement
def someFunction(m: Map[Int, List[String]]): Map[List[Int], List[String]] =
// stuff that would turn x into
// Map(List(1, 3) -> List("a", "b"), List(2, 4) -> List("a"), List(5) -> List("c"))
?
You can convert the Map to a List and then use groupBy to aggregate the first element of each tuple:
x.toList.groupBy(_._2).mapValues(_.map(_._1)).map{ case (x, y) => (y, x) }
// res37: scala.collection.immutable.Map[List[Int],List[String]] =
// Map(List(2, 4) -> List(a), List(1, 3) -> List(a, b), List(5) -> List(c))
Or as #Dylan commented, use _.swap to switch the tuples' elements:
x.toList.groupBy(_._2).mapValues(_.map(_._1)).map(_.swap)

Scala flatten nested map

I have a nested Map like this one:
Map(1 -> Map(2 -> 3.0, 4 -> 5.0), 6 -> Map(7 -> 8.0))
I would like to 'flatten' it in a way such that the keys of the outer and inner maps are paired, i.e. for the example above:
Seq((1,2),(1,4),(6,7))
what is an elegant way to do this?
val m = Map(1 -> Map(2 -> 3.0, 4 -> 5.0), 6 -> Map(7 -> 8.0))
m.toSeq.flatMap({case (k, v) => v.keys.map((k,_))})
With for-comprehension:
val m = Map(1 -> Map(2 -> 3.0, 4 -> 5.0), 6 -> Map(7 -> 8.0))
scala> for((k1, v1) <- m.toSeq; k2 <- v1.keys) yield (k1, k2)
res4: Seq[(Int, Int)] = ArrayBuffer((1,2), (1,4), (6,7))