How to find all users that are in a location

53 views Asked by At

I have a collection of users:

case class User(id: Int, locationId: Int)

val users: List[User] = ....

val locationIds = List(1231,34323,3452)

How can I find all users that are in the locationIds?

val usersInLocation = users.map(_.locationId == ??)
2

There are 2 answers

0
Tanjin On BEST ANSWER

How about:

users.filter(user => locationIds.contains(user.locationId))
1
Jojo Narte On

Filter would probably be useful val usersInLocation = users.filter(_.locationId == 1) Let's say you have: Users[{name:'john', locationId: 1}, {name: 'mike', locationId: 2}, {name: 'jenny', locationId: 1}]

With the filter above usersInLocation would result in usersInLocation = [{name:'john', locationId: 1}, {name: 'jenny', locationId: 1}]