Skip to content

Instantly share code, notes, and snippets.

@plumpNation
Created December 16, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plumpNation/6dc3cfa592a5ed8bc0a0bb888498ac8b to your computer and use it in GitHub Desktop.
Save plumpNation/6dc3cfa592a5ed8bc0a0bb888498ac8b to your computer and use it in GitHub Desktop.
A quick demo of opportunities for chaining from optionals, through nested classes, and onto streams from the result
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Scratch {
public static void main(String[] args) {
Optional<Schmoochies> schmoochies = Optional.of(new Schmoochies());
List<Integer> output = schmoochies
.flatMap(Schmoochies::getManoochies)
.map(Manoochies::getMaNumber)
.orElse(Collections.singletonList(0))
.stream()
.map(n -> n + 4)
.collect(Collectors.toList());
if (output.get(0) == 9) {
System.out.println("YAY");
}
}
}
class Schmoochies {
Optional<Manoochies> getManoochies() {
return Optional.of(new Manoochies());
}
}
class Manoochies {
public List<Integer> getMaNumber() {
return Collections.singletonList(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment