Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Last active December 18, 2016 12:08
Show Gist options
  • Select an option

  • Save ru-rocker/0ca9d5c80d5a6bc3b845ea1a0b86e664 to your computer and use it in GitHub Desktop.

Select an option

Save ru-rocker/0ca9d5c80d5a6bc3b845ea1a0b86e664 to your computer and use it in GitHub Desktop.
Example of using stream flatMap function
// 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