Last active
December 18, 2016 12:08
-
-
Save ru-rocker/0ca9d5c80d5a6bc3b845ea1a0b86e664 to your computer and use it in GitHub Desktop.
Example of using stream flatMap function
This file contains hidden or 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
| // list of negative number | |
| List<Integer> negList = Arrays.asList(-4, -3, -2, -1); | |
| // list of positive number | |
| List<Integer> posList = Arrays.asList(1, 2, 3, 4); | |
| // find the odd number in both lists with single stream operation | |
| List<Integer> collector = Stream.of(negList, posList) | |
| .flatMap(List::stream) //transform into stream of other objects | |
| .filter(i -> i % 2 != 0) | |
| .collect(Collectors.toList()); | |
| System.out.println(collector); | |
| //Output: [-3, -1, 1, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment