Skip to content

Instantly share code, notes, and snippets.

@Jayasagar
Created December 17, 2017 15:15
Show Gist options
  • Save Jayasagar/3bf35331ce165c0c1193d6a294da5a67 to your computer and use it in GitHub Desktop.
Save Jayasagar/3bf35331ce165c0c1193d6a294da5a67 to your computer and use it in GitHub Desktop.
consumerList
.stream()
// map intermediate operation
// 'map' is used to transform one element type into other, here we transformed Consumer into String
.map(Consumer::getName)
// 'distinct' intermediate operation
.distinct()
// 'sorted' intermediate operation
.sorted()
// 'reduce' terminal operation
// 'reduce' is used to produce single output
// .reduce(String::concat);
.reduce((preVName, nextName) -> preVName + " " + nextName)
.ifPresent((result) -> System.out.println(result));
// Outout: Bob Malli Satti Sri
// In above code you can also use 'reduce(String::concat)'
// Please read Optional to understand more on ifPresent method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment