Streams Java8: Flat Map usage
public static void fetchUsersWhoseAddressIsOne(){
List<User> users1 = formUsers().stream()
.filter(employee -> employee.getAddresses().stream()
.anyMatch(adr -> adr.getId().startsWith("1")))
.collect(Collectors.toList());
System.out.println(users1);
}
public static void flatMapFetchAddresses(){
List<Address> addresses =
formUsers().stream()
.map(User::getAddresses).flatMap(Collection::stream)
.filter(ad -> ad.getId().equals("1")).collect(Collectors.toList());
System.out.println(addresses);
}
Comments
Post a Comment