我有一个与此相似的对象模型:
List<User> userList; //and each user contains list of address
List<Address>;
class User {
private Long id;
List<Address> address;
// getters & setters
}
class Address {
Long id
}
现在,我需要从Java Stream API的4
中找到预定义地址ID的用户,例如userList
。
答案 0 :(得分:1)
Optional<User> user = userList.stream()
.filter(x -> x.getAddress().stream().anyMatch(a -> a.getId() == 4))
.findAny();