Created
February 26, 2015 08:21
-
-
Save ndeloof/4ed2f5c8c02f19dc5b6e to your computer and use it in GitHub Desktop.
to stream or not to stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Groovy: