gpt4 book ai didi

java - Guava Maps.Scala map 上的差异

转载 作者:行者123 更新时间:2023-12-02 08:59:03 26 4
gpt4 key购买 nike

尝试执行以下操作,但是获得与替代方案不同的重载方法值:无法应用于 java.util.Map[String, Any]


import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.google.common.collect.Maps

import scala.collection.JavaConverters._



object Testing extends App {

val leftJson = """{
"name": {
"first": "John",
"last": "Doe"
},
"address": null,
"birthday": "1980-01-01",
"company": "Acme",
"occupation": "Software engineer",
"phones": [
{
"number": "000000000",
"type": "home"
},
{
"number": "999999999",
"type": "mobile"
}
]
}"""
val rightJson = """{
"name": {
"first": "Jane",
"last": "Doe",
"nickname": "Jenny"
},
"birthday": "1990-01-01",
"occupation": null,
"phones": [
{
"number": "111111111",
"type": "mobile"
}
],
"favorite": true,
"groups": [
"close-friends",
"gym"
]
}"""

val mapper = new ObjectMapper
mapper.registerModule(DefaultScalaModule)
val leftMap = mapper.readValue(leftJson, classOf[Map[String, Any]])
val rightMap = mapper.readValue(rightJson, classOf[Map[String, Any]])

val differences = Maps.difference(leftMap.asJava, rightMap.asJava)

}
Error:(59, 26) overloaded method value difference with alternatives:
[K, V](x$1: java.util.SortedMap[K, _ <: V], x$2: java.util.Map[_ <: K, _ <: V])com.google.common.collect.SortedMapDifference[K,V] <and>
[K, V](x$1: java.util.Map[_ <: K, _ <: V], x$2: java.util.Map[_ <: K, _ <: V], x$3: com.google.common.base.Equivalence[_ >: V])com.google.common.collect.MapDifference[K,V] <and>
[K, V](x$1: java.util.Map[_ <: K, _ <: V], x$2: java.util.Map[_ <: K, _ <: V])com.google.common.collect.MapDifference[K,V]
cannot be applied to (java.util.Map[String,Object], java.util.Map[String,Object])
val differences = Maps.difference(leftMap.asJava, rightMap.asJava)

最佳答案

好消息是您的代码可以使用 Scala 2.13.1 正确编译。

我还尝试使用 Scala 2.12.10 编译您的代码,确实出现了问题。

如果您向编译器提示类型参数,问题就会得到解决:

val differences = Maps.difference[String, Any](leftMap.asJava, rightMap.asJava)

关于java - Guava Maps.Scala map 上的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304853/

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