反转 Map 最简洁的 Scala 方法是什么?

发布于 2024-09-18 07:12:59 字数 177 浏览 3 评论 0原文

反转 Map 最简洁的 Scala 方法是什么? 地图可能包含非唯一值。

编辑:

Map[A, B] 的反转应该给出 Map[B, Set[A]](或者 MultiMap,那会更好)。

What is the most succinct Scala way to reverse a Map? The Map may contain non-unique values.

EDIT:

The reversal of Map[A, B] should give Map[B, Set[A]] (or a MultiMap, that would be even better).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-09-25 07:13:00

如果您可能会丢失重复的键:

scala> val map = Map(1->"one", 2->"two", -2->"two")
map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (-2,two))

scala> map.map(_ swap)
res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,-2))

如果您不想作为多重映射进行访问,而只想以集合的映射的形式进行访问,则:

scala> map.groupBy(_._2).mapValues(_.keys.toSet)
res1: scala.collection.immutable.Map[
  java.lang.String,scala.collection.immutable.Set[Int]
] = Map((one,Set(1)), (two,Set(2, -2)))

如果您坚持要获取MultiMap,则:

scala> import scala.collection.mutable.{HashMap, Set, MultiMap}
scala> ( (new HashMap[String,Set[Int]] with MultiMap[String,Int]) ++=
     |          map.groupBy(_._2).mapValues(Set[Int]() ++= _.keys) )
res2: scala.collection.mutable.HashMap[String,scala.collection.mutable.Set[Int]]
with scala.collection.mutable.MultiMap[String,Int] = Map((one,Set(1)), (two,Set(-2, 2)))

If you can lose duplicate keys:

scala> val map = Map(1->"one", 2->"two", -2->"two")
map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (-2,two))

scala> map.map(_ swap)
res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,-2))

If you don't want access as a multimap, just a map to sets, then:

scala> map.groupBy(_._2).mapValues(_.keys.toSet)
res1: scala.collection.immutable.Map[
  java.lang.String,scala.collection.immutable.Set[Int]
] = Map((one,Set(1)), (two,Set(2, -2)))

If you insist on getting a MultiMap, then:

scala> import scala.collection.mutable.{HashMap, Set, MultiMap}
scala> ( (new HashMap[String,Set[Int]] with MultiMap[String,Int]) ++=
     |          map.groupBy(_._2).mapValues(Set[Int]() ++= _.keys) )
res2: scala.collection.mutable.HashMap[String,scala.collection.mutable.Set[Int]]
with scala.collection.mutable.MultiMap[String,Int] = Map((one,Set(1)), (two,Set(-2, 2)))
把昨日还给我 2024-09-25 07:13:00
scala> val m1 = Map(1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four")
m1: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (3,three), (4,four))

scala> m1.map(pair => pair._2 -> pair._1)
res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,2), (three,3), (four,4))

编辑澄清的问题:

object RevMap {
  def
  main(args: Array[String]): Unit = {
    val m1 = Map("one" -> 3, "two" -> 3, "three" -> 5, "four" -> 4, "five" -> 5, "six" -> 3)

    val rm1 = (Map[Int, Set[String]]() /: m1) { (map: Map[Int, Set[String]], pair: (String, Int)) =>
                                                 map + ((pair._2, map.getOrElse(pair._2, Set[String]()) + pair._1)) }

    printf("m1=%s%nrm1=%s%n", m1, rm1)
  }
}

% scala RevMap
m1=Map(four -> 4, three -> 5, two -> 3, six -> 3, five -> 4, one -> 3)
rm1=Map(4 -> Set(four, five), 5 -> Set(three), 3 -> Set(two, six, one))

我不确定这是否足够简洁。

scala> val m1 = Map(1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four")
m1: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (3,three), (4,four))

scala> m1.map(pair => pair._2 -> pair._1)
res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,2), (three,3), (four,4))

Edit for clarified question:

object RevMap {
  def
  main(args: Array[String]): Unit = {
    val m1 = Map("one" -> 3, "two" -> 3, "three" -> 5, "four" -> 4, "five" -> 5, "six" -> 3)

    val rm1 = (Map[Int, Set[String]]() /: m1) { (map: Map[Int, Set[String]], pair: (String, Int)) =>
                                                 map + ((pair._2, map.getOrElse(pair._2, Set[String]()) + pair._1)) }

    printf("m1=%s%nrm1=%s%n", m1, rm1)
  }
}

% scala RevMap
m1=Map(four -> 4, three -> 5, two -> 3, six -> 3, five -> 4, one -> 3)
rm1=Map(4 -> Set(four, five), 5 -> Set(three), 3 -> Set(two, six, one))

I'm not sure this qualifies as succinct.

自控 2024-09-25 07:13:00

怎么样:

  implicit class RichMap[A, B](map: Map[A, Seq[B]])
  {
    import scala.collection.mutable._

    def reverse: MultiMap[B, A] =
    {
      val result = new HashMap[B, Set[A]] with MultiMap[B, A]

      map.foreach(kv => kv._2.foreach(result.addBinding(_, kv._1)))

      result
    }
  }

或者

  implicit class RichMap[A, B](map: Map[A, Seq[B]])
  {
    import scala.collection.mutable._

    def reverse: MultiMap[B, A] =
    {
      val result = new HashMap[B, Set[A]] with MultiMap[B, A]

      map.foreach{case(k,v) => v.foreach(result.addBinding(_, k))}

      result
    }
  }

How about:

  implicit class RichMap[A, B](map: Map[A, Seq[B]])
  {
    import scala.collection.mutable._

    def reverse: MultiMap[B, A] =
    {
      val result = new HashMap[B, Set[A]] with MultiMap[B, A]

      map.foreach(kv => kv._2.foreach(result.addBinding(_, kv._1)))

      result
    }
  }

or

  implicit class RichMap[A, B](map: Map[A, Seq[B]])
  {
    import scala.collection.mutable._

    def reverse: MultiMap[B, A] =
    {
      val result = new HashMap[B, Set[A]] with MultiMap[B, A]

      map.foreach{case(k,v) => v.foreach(result.addBinding(_, k))}

      result
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文