Skip to content

Instantly share code, notes, and snippets.

@hakaneroztekin
Last active August 23, 2019 13:01
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 hakaneroztekin/498408c073db7a9d6b93957974f12419 to your computer and use it in GitHub Desktop.
Save hakaneroztekin/498408c073db7a9d6b93957974f12419 to your computer and use it in GitHub Desktop.
public class StreamTerminalOperations {
// collect: The collect method is used to return
// the result of the intermediate operations performed on the stream.
List number = Arrays.asList(2,3,4,5,3);
Set square = number.stream().map(x->x*x).collect(Collectors.toSet());
//forEach: The forEach method is used to
// iterate through every element of the stream.
List number = Arrays.asList(2,3,4,5);
number.stream().map(x->x*x).forEach(y->System.out.println(y));
// reduce: The reduce method is used
// to reduce the elements of a stream to a single value.
// The reduce method takes a BinaryOperator as a parameter.
List number = Arrays.asList(2,3,4,5);
// Here ans variable is assigned 0 as the initial value and i is added to it .
int even = number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment