当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala mutable.MultiMap用法及代码示例


@deprecated(“在scala-collection-contrib模块中使用scala.collection.mutable.MultiDict”,"2.13.0")

用法:

trait MultiMap[K, V] extends Map[K, Set[V]]

具有分配给键的多个值的可变映射的特征。

此类通常用作 mixin。它将映射KSet[V] 对象的映射转换为映射KV 对象的多映射。

例子:

// first import all necessary types from package `collection.mutable`
import collection.mutable.{ HashMap, MultiMap, Set }
// to create a `MultiMap` the easiest way is to mixin it into a normal
// `Map` instance
val mm = new HashMap[Int, Set[String]] with MultiMap[Int, String]
// to add key-value pairs to a multimap it is important to use
// the method `addBinding` because standard methods like `+` will
// overwrite the complete key-value pair instead of adding the
// value to the existing key
mm.addBinding(1, "a")
mm.addBinding(2, "b")
mm.addBinding(1, "c")
// mm now contains `Map(2 -> Set(b), 1 -> Set(c, a))`
// to check if the multimap contains a value there is method
// `entryExists`, which allows to traverse the including set
mm.entryExists(1, _ == "a") == true
mm.entryExists(1, _ == "b") == false
mm.entryExists(2, _ == "b") == true
// to remove a previous added value there is the method `removeBinding`
mm.removeBinding(1, "a")
mm.entryExists(1, _ == "a") == false

已弃用

源码:

MultiMap.scala

相关用法


注:本文由纯净天空筛选整理自scala-lang.org大神的英文原创作品 mutable.MultiMap。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。