Created
December 16, 2021 14:31
-
-
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
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
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