Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 251
blue-sky Avatar asked Mar 26 '26 21:03

blue-sky


1 Answers

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.

like image 157
Jean-Philippe Pellet Avatar answered Mar 29 '26 11:03

Jean-Philippe Pellet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!