我有一组用户:
case class User(id: Int, locationId: Int)
val users: List[User] = ....
val locationIds = List(1231,34323,3452)
如何找到locationIds中的所有用户?
val usersInLocation = users.map(_.locationId == ??)
答案 0 :(得分:2)
怎么样:
users.filter(user => locationIds.contains(user.locationId))
答案 1 :(得分:0)
过滤器可能会有用 val usersInLocation = users.filter(_。locationId == 1) 假设你有: 用户[{name:'john',locationId:1},{name:'mike',locationId:2},{name:'jenny',locationId:1}]
使用userInLocation上面的过滤器会导致 usersInLocation = [{name:'john',locationId:1},{name:'jenny',locationId:1}]