Skip to content

Instantly share code, notes, and snippets.

@bradynpoulsen
Created June 27, 2018 21:38
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 bradynpoulsen/f3f5458d718a996c5b3dec9decefb6d2 to your computer and use it in GitHub Desktop.
Save bradynpoulsen/f3f5458d718a996c5b3dec9decefb6d2 to your computer and use it in GitHub Desktop.
Compares the difference between map and flatMap in Java 8
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