Last active
August 23, 2019 13:01
-
-
Save hakaneroztekin/498408c073db7a9d6b93957974f12419 to your computer and use it in GitHub Desktop.
This file contains 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
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