gpt4 book ai didi

scala - 如何为我的自定义 mutable.HashMap 编写自定义方法++=

转载 作者:行者123 更新时间:2023-12-04 17:59:53 32 4
gpt4 key购买 nike

我编写了如下所示的映射,即我的 mutable.HashMap 实现。

class SampleMap() extends mutable.HashMap[String, (Any, BigInt)]

并且重写了map的+=方法,其余方法均来自父类(super class)而不是重写。下面完美运行。

override def +=(kv: (String, (Any, BigInt))): this.type = {/*compiled code*/}

现在我想编写++= 方法的自定义实现,该方法是由 Map 从可增长类继承的。当我编写重写方法++= 时,编译器提示没有任何需要重写的内容。

override def ++=(currentMap: MergeMap): this.type  = {

如何为我的自定义 map 编写此方法的自定义实现。

最佳答案

尝试覆盖scala.collection.generic.Growable.++=方法:

class SampleMap() extends mutable.HashMap[String, (Any, BigInt)] {
override def ++=(xs: TraversableOnce[(String, (Any, BigInt))]): this.type = {
this
}
}

然后效果很好:

def main(args: Array[String]): Unit = {
new SampleMap() ++= mutable.HashMap.empty[String, (Any, BigInt)]
}

关于scala - 如何为我的自定义 mutable.HashMap 编写自定义方法++=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55934480/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com