Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Forked from novoj/Lambda formatting
Last active August 29, 2015 14:11
Show Gist options
  • Save tomaszalusky/851beeaab86eb2c37a9c to your computer and use it in GitHub Desktop.
Save tomaszalusky/851beeaab86eb2c37a9c to your computer and use it in GitHub Desktop.
private Map<Integer,? extends Address> getAddressIndex(Optional<SubjectData> currentSubjectData) {
return currentSubjectData
.map(subjectData -> subjectData
.getAddresses()
.stream()
.filter(address -> address.getEsoId() != null)
.collect(toMap(Address::getEsoId, t -> t))
)
.orElse(emptyMap())
;
}
// HYPOTHETICAL SOLUTION WITH GUAVA Optional:
private Map<Integer,? extends Address> getAddressIndex(Optional<SubjectData> currentSubjectData) {
return currentSubjectData
.asSet() // Guava Optional's method, in JDK8 must be simulated with map(...).get()
.stream()
.flatMap(sd -> sd.getAddresses().stream())
.filter(address -> address.getEsoId() != null)
.collect(toMap(Address::getEsoId, t -> t))
;
//.orElse(emptyMap()); // unnecessary here, asSet did the work
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment