Created
June 27, 2018 21:38
-
-
Save bradynpoulsen/f3f5458d718a996c5b3dec9decefb6d2 to your computer and use it in GitHub Desktop.
Compares the difference between map and flatMap in Java 8
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.function.Function; | |
import java.util.stream.Stream; | |
public class Foo { | |
void test() { | |
Function<Integer, Stream<Integer>> transformer = it -> Stream.of(it * 10, it * 100, it * 1000); | |
Stream<Integer> myInts = Stream.of(1, 2, 3); | |
// contains [ [10, 100, 1000], [20, 200, 2000], [30, 300, 3000] ] | |
Stream<Stream<Integer>> nestedInts = myInts.map(transformer); | |
// contains [ 10, 100, 1000, 20, 200, 2000, 30, 300, 3000 ] | |
Stream<Integer> flatInts = myInts.flatMap(transformer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment