Shashwat Mishra
Shashwat Mishra

Reputation: 96

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]]. One would expect e to have the type 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]]

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 ?

Upvotes: 0

Views: 145

Answers (1)

Shashwat Mishra
Shashwat Mishra

Reputation: 96

That was a stupid mistake. As Lukasz commented on the question, scala lambdas cannot replace 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.

Upvotes: 0

Related Questions