使用谓词从 scala 可变映射中删除元素的正确方法是什么

发布于 2024-08-26 05:37:01 字数 257 浏览 7 评论 0原文

如何在不创建任何新集合的情况下做到这一点?还有比这更好的事情吗?

val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4)
m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1))
println(m)

Scala 2.8 中的 PS

How to do that without creating any new collections? Is there something better than this?

val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4)
m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1))
println(m)

P.S. in Scala 2.8

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

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

发布评论

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

评论(4

愛放△進行李 2024-09-02 05:37:01

retain 做你想要的。在 2.7 中:

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

scala> a.retain((k,v) => v.length < 4)   

scala> a
res0: scala.collection.mutable.Map[Int,java.lang.String] =
  Map(2 -> two, 1 -> one)

它也有效,但我认为在 2.8 中仍在不断变化。

retain does what you want. In 2.7:

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

scala> a.retain((k,v) => v.length < 4)   

scala> a
res0: scala.collection.mutable.Map[Int,java.lang.String] =
  Map(2 -> two, 1 -> one)

It also works, but I think is still in flux, in 2.8.

翻身的咸鱼 2024-09-02 05:37:01

根据 Scala 可变映射参考页面,您可以使用 -= 或remove 删除单个元素,如下所示:

val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4)
m -= "1" // returns m
m.remove("2") // returns Some(2)

区别在于 -= 返回原始映射对象,而remove 返回一个 Option,其中包含与删除的键对应的值(如果有的话)。

当然,正如其他答案所示,如果您想根据条件删除许多元素,您应该考虑保留、过滤等。

Per the Scala mutable map reference page, you can remove a single element with either -= or remove, like so:

val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4)
m -= "1" // returns m
m.remove("2") // returns Some(2)

The difference is that -= returns the original map object, while remove returns an Option containing the value corresponding to the removed key (if there was one.)

Of course, as other answers indicate, if you want to remove many elements based on a condition, you should look into retain, filter, etc.

_蜘蛛 2024-09-02 05:37:01

如果您使用 immutable.Map,在 2.7 中,它可能必须类似于:

def pred(k: Int, v: String) = k % 2 == 0

m --= (m.filter(pred(_, _)).keys

因为没有可用的 retain 方法。显然在这种情况下 m 必须声明为 var

If you are using an immutable.Map, in 2.7 it might have to be something like:

def pred(k: Int, v: String) = k % 2 == 0

m --= (m.filter(pred(_, _)).keys

As there is no retain method available. Obviously in this case m must be declared as a var

野生奥特曼 2024-09-02 05:37:01

对于 scala version=2.13.6 使用 filterInPlace

文档链接

@ val a = collection.mutable.Map(1->"one",2->"two",3->"three")
a: collection.mutable.Map[Int, String] = HashMap(
  1 -> "one",
  2 -> "two",
  3 -> "three"
)

@ a.filterInPlace((k,v) => v.length < 4)
res1: collection.mutable.Map[Int, String] = HashMap(1 -> "one", 2 -> "two")

@ a
res2: collection.mutable.Map[Int, String] = HashMap(1 -> "one", 2 -> "two")

for scala version=2.13.6 use filterInPlace

doc link

@ val a = collection.mutable.Map(1->"one",2->"two",3->"three")
a: collection.mutable.Map[Int, String] = HashMap(
  1 -> "one",
  2 -> "two",
  3 -> "three"
)

@ a.filterInPlace((k,v) => v.length < 4)
res1: collection.mutable.Map[Int, String] = HashMap(1 -> "one", 2 -> "two")

@ a
res2: collection.mutable.Map[Int, String] = HashMap(1 -> "one", 2 -> "two")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文