Skip to content

Instantly share code, notes, and snippets.

@ndeloof
Created February 26, 2015 08:21
Show Gist options
  • Save ndeloof/4ed2f5c8c02f19dc5b6e to your computer and use it in GitHub Desktop.
Save ndeloof/4ed2f5c8c02f19dc5b6e to your computer and use it in GitHub Desktop.
to stream or not to stream
// What I used to do in Java 6/7
Collection<String> accountIds = new ArrayList<>();
for (GCAccount account : user.getGcAccounts()) {
for (Account sf : account.getSfAccounts()) {
accountIds.add(sf.getId());
}
}
// What I can do in Java 8
Collection<String> accountIds =
user.getGcAccounts().stream()
.flatMap(gc -> gc.getSfAccounts().stream())
.map(Account::getId)
.collect(Collectors.toList());
// What I expected I could do in Java 8
Collection<String> accountIds =
user.getGcAccounts().stream() // why not sequential() by default and offer parallel() on Collection ?
.flatMap(GcAccount::getSfAccounts)
.map(Account::getId)
.toList();
@melix
Copy link

melix commented Feb 26, 2015

In Groovy:

users.gcAccounts.sfAccounts.id.flatten()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment