[英]Inferred parameter types in scala
The following code generates type mismatch error. 以下代码生成类型不匹配错误。 I dont't understand why. 我不明白为什么。
var navigablemap=rst.getFamilyMap("a".getBytes())
var list = new ListBuffer[Long]()
navigablemap.keySet().forEach((e:Array[Byte]) => list+=Bytes.toLong(e))
navigablemap has the type NavigableMap[Array[Byte], Array[Byte]] . navigablemap的类型为NavigableMap [Array [Byte],Array [Byte]] 。 One would expect e to have the type Array[Byte] 人们会期望e的类型为Array [Byte]
Compiler reports the following error message. 编译器报告以下错误信息。
type mismatch; 类型不匹配; found : Array[Byte] ⇒ list.type (with underlying type Array[Byte] ⇒ list.type) required: java.util.function.Consumer[_ >: Array[Byte]] 找到:Array [Byte]⇒list.type(具有基础类型Array [Byte]⇒list.type)必需:java.util.function.Consumer [_>:Array [Byte]]
Update: The following works. 更新:以下工作。
var keys=navigablemap.keySet()
var keysIterator=keys.iterator()
while (keysIterator.hasNext){
var e=keysIterator.next()
list+=Bytes.toLong(e)
}
Since brevity is one of my goals while I try to learn scala, is there a scala-one-liner for the above ? 由于简洁是我尝试学习scala的目标之一,因此上述内容是否有scala-one-liner?
That was a stupid mistake. 那是一个愚蠢的错误。 As Lukasz commented on the question, scala lambdas cannot replace java lambda. 正如Lukasz对这个问题的评论一样,scala lambdas不能替代java lambda。 For future references, this is the working solution. 供以后参考,这是有效的解决方案。
var navigablemap=rst.getFamilyMap("a".getBytes())
val list = new ListBuffer[Long]()
var keys=navigablemap.keySet()
val scalaKeys=asScalaSet(keys)
scalaKeys.foreach(x => list+=Bytes.toLong(x))
The conversion asScalaSet
is important. 转换asScalaSet
很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.