Pergunta

I have list of users of type :

java.util.List[User] 

User is of type :

case class User(id: String, type : BigInt) 

I want to filter into a List of Strings where each String is the id of the user : java.util.List[String]

I could just iterate over each value in the List and just select the id and populate the new List.

Can I use Scala's filter functionality to achieve this?

Foi útil?

Solução

You'll have to convert to a Scala collection and then back to a Java collection:

import collection.JavaConverters._

val l: java.util.List[User] = ...
val l2 = l.asScala.map(_.id).asJava
// l2: java.util.List[java.lang.String] = ...

Alternatively, you may be able to write your own collection builders and CanBuildFrom implicits for the Java collections, but this would clearly be more work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top