blue-sky
blue-sky

Reputation: 53876

How to filter object data?

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?

Upvotes: 3

Views: 124

Answers (1)

Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 60006

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.

Upvotes: 4

Related Questions