Reputation: 2628
From java.util.List[CustomObject] I want to remove value and create a comma separated string, tried the below code but got exception
snippet 1
def getVal(a_attribList: List[CustomObject]): String = a_attribList
.stream()
.map(new java.util.function.Function[CustomObject, String] {
override def apply(CustomObject): String = {
t.getNolumn
}
})
.collect(Collectors.joining(","))
snippet 2
def getVal(a_attribList: List[CustomObject]): String = {
a_attribList
.stream()
.map(a => a.getNolumn)
.collect(Collectors.joining(","));
}
Exception
type mismatch;
found : java.util.function.Function[com.test.dataobjects.CustomObject,String]
required: java.util.function.Function[_ >: com.test.dataobjects.CustomObject, _ <: R]
.map(a => a.getNolumn)
^
three errors found
Upvotes: 1
Views: 9249
Reputation: 22895
As @ValentinCarnu already pointed out the snippets are using scala.collection.immutable.List
instead of a java.util.List
.
Thus, you only need to map it using an standar scala anonymous function, and then convert it to a String using the mkString method
def getVal(a_attribList: List[CustomObject]): String =
a_attribList.map(a => a.getNolumn).mkString(",")
Now, if you really have a java List, you can convert it to a Scala list using the JavaConverters package.
import scala.collection.JavaConverters._
def getVal(a_attribList: java.util.List[CustomObject]): String =
a_attribList.asScala.map(a => a.getNolumn).mkString(",")
Upvotes: 3
Reputation: 11739
You can covert it to scala List and do the mapping:
a_attribList.asScala.map(_.getNolumn).mkString(",")
Upvotes: 2