Skip to content

Instantly share code, notes, and snippets.

@invkrh
Last active April 23, 2019 11:46
Show Gist options
  • Save invkrh/e22167dfb73fcfff606e387504d6b709 to your computer and use it in GitHub Desktop.
Save invkrh/e22167dfb73fcfff606e387504d6b709 to your computer and use it in GitHub Desktop.
case class Product(id: Int, tags: Set[String])
val productList = Array(Product(0, Set("a")), Product(1, Set("a", "b")))
val tagDict1: Map[String, mutable.HashSet[Product]] = productList.
flatMap(p => p.tags.map(_ -> p)).
groupBy(_._1).
mapValues(v => mutable.HashSet() ++ v.map(_._2))
val tagDict2 = Map[String, mutable.HashSet[Product]](
"a" -> mutable.HashSet(productList(0), productList(1)),
"b" -> mutable.HashSet(productList(1)))
println(tagDict1 == tagDict2)
println(tagDict1("a") eq tagDict1("a"))
println(tagDict2("a") eq tagDict2("a"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment