Convert List of tuple to map (and deal with duplicate key ?)

ScalaMap

Scala Problem Overview


I was thinking about a nice way to convert a List of tuple with duplicate key [("a","b"),("c","d"),("a","f")] into map ("a" -> ["b", "f"], "c" -> ["d"]). Normally (in python), I'd create an empty map and for-loop over the list and check for duplicate key. But I am looking for something more scala-ish and clever solution here.

btw, actual type of key-value I use here is (Int, Node) and I want to turn into a map of (Int -> NodeSeq)

Scala Solutions


Solution 1 - Scala

For Googlers that don't expect duplicates or are fine with the default duplicate handling policy:

List("a" -> 1, "b" -> 2, "a" -> 3).toMap
// Result: Map(a -> 3, c -> 2)

As of 2.12, the default policy reads:

> Duplicate keys will be overwritten by later keys: if this is an unordered collection, which key is in the resulting map is undefined.

Solution 2 - Scala

Group and then project:

scala> val x = List("a" -> "b", "c" -> "d", "a" -> "f")
//x: List[(java.lang.String, java.lang.String)] = List((a,b), (c,d), (a,f))
scala> x.groupBy(_._1).map { case (k,v) => (k,v.map(_._2))}
//res1: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(c -> List(d), a -> List(b, f))

More scalish way to use fold, in the way like there (skip map f step).

Solution 3 - Scala

Here's another alternative:

x.groupBy(_._1).mapValues(_.map(_._2))

Solution 4 - Scala

For Googlers that do care about duplicates:

implicit class Pairs[A, B](p: List[(A, B)]) {
  def toMultiMap: Map[A, List[B]] = p.groupBy(_._1).mapValues(_.map(_._2))
}

> List("a" -> "b", "a" -> "c", "d" -> "e").toMultiMap
> Map("a" -> List("b", "c"), "d" -> List("e")) 

Solution 5 - Scala

Starting Scala 2.13, most collections are provided with the groupMap method which is (as its name suggests) an equivalent (more efficient) of a groupBy followed by mapValues:

List("a" -> "b", "c" -> "d", "a" -> "f").groupMap(_._1)(_._2)
// Map[String,List[String]] = Map(a -> List(b, f), c -> List(d))

This:

  • groups elements based on the first part of tuples (group part of groupMap)

  • maps grouped values by taking their second tuple part (map part of groupMap)

This is an equivalent of list.groupBy(_._1).mapValues(_.map(_._2)) but performed in one pass through the List.

Solution 6 - Scala

Below you can find a few solutions. (GroupBy, FoldLeft, Aggregate, Spark)

val list: List[(String, String)] = List(("a","b"),("c","d"),("a","f"))

GroupBy variation

list.groupBy(_._1).map(v => (v._1, v._2.map(_._2)))

Fold Left variation

list.foldLeft[Map[String, List[String]]](Map())((acc, value) => {
  acc.get(value._1).fold(acc ++ Map(value._1 -> List(value._2))){ v =>
    acc ++ Map(value._1 -> (value._2 :: v))
  }
})

Aggregate Variation - Similar to fold Left

list.aggregate[Map[String, List[String]]](Map())(
  (acc, value) => acc.get(value._1).fold(acc ++ Map(value._1 -> 
    List(value._2))){ v =>
     acc ++ Map(value._1 -> (value._2 :: v))
  },
  (l, r) => l ++ r
)

Spark Variation - For big data sets (Conversion to a RDD and to a Plain Map from RDD)

import org.apache.spark.rdd._
import org.apache.spark.{SparkContext, SparkConf}

val conf: SparkConf = new 
SparkConf().setAppName("Spark").setMaster("local")
val sc: SparkContext = new SparkContext (conf)

// This gives you a rdd of the same result
val rdd: RDD[(String, List[String])] = sc.parallelize(list).combineByKey(
   (value: String) => List(value),
   (acc: List[String], value) => value :: acc,
   (accLeft: List[String], accRight: List[String]) => accLeft ::: accRight
)

// To convert this RDD back to a Map[(String, List[String])] you can do the following
rdd.collect().toMap

Solution 7 - Scala

Here is a more Scala idiomatic way to convert a list of tuples to a map handling duplicate keys. You want to use a fold.

val x = List("a" -> "b", "c" -> "d", "a" -> "f")

x.foldLeft(Map.empty[String, Seq[String]]) { case (acc, (k, v)) =>
  acc.updated(k, acc.getOrElse(k, Seq.empty[String]) ++ Seq(v))
}

res0: scala.collection.immutable.Map[String,Seq[String]] = Map(a -> List(b, f), c -> List(d))

Solution 8 - Scala

You can try this

scala> val b = new Array[Int](3)
// b: Array[Int] = Array(0, 0, 0)
scala> val c = b.map(x => (x -> x * 2))
// c: Array[(Int, Int)] = Array((1,2), (2,4), (3,6))
scala> val d = Map(c : _*)
// d: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 2 -> 4, 3 -> 6)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTg.View Question on Stackoverflow
Solution 1 - ScalaCory KleinView Answer on Stackoverflow
Solution 2 - Scalaom-nom-nomView Answer on Stackoverflow
Solution 3 - ScalaDaniel C. SobralView Answer on Stackoverflow
Solution 4 - ScalapathikritView Answer on Stackoverflow
Solution 5 - ScalaXavier GuihotView Answer on Stackoverflow
Solution 6 - ScalaMelcom van EedenView Answer on Stackoverflow
Solution 7 - ScalacevarisView Answer on Stackoverflow
Solution 8 - ScalafrankfzwView Answer on Stackoverflow