Blankman
Blankman

Reputation: 267230

How to find all users that are in a location

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 == ??)

Upvotes: 0

Views: 44

Answers (2)

Tanjin
Tanjin

Reputation: 2452

How about:

users.filter(user => locationIds.contains(user.locationId))

Upvotes: 2

Jojo Narte
Jojo Narte

Reputation: 3104

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}]

Upvotes: 0

Related Questions