This section discussed about the the 2 major concepts in Java programming language.
map(lambdaMapper):
Used to returns a stream consisting of the results of applying the given function to the elements of this stream.List<Integer> numbers = Arrays.asList(2,3,4,5);
List<Integer> squares = numbers.stream().map(e -> e*e).collect(Collectors.toList());
filter(lambdaFilterer):
- Used to select elements as per the Predicate passed as argument.List<String> names = Arrays.asList("Reflection", "Collection", "Stream");
List<String> filteredNames = names.stream().filter(e -> e.startsWith("S")).collect(Collectors.toList());
sorted(lambdaComparator):
- Used to sort the stream.List<String> names = Arrays.asList("Reflection", "Collection", "Stream");
List<String> sortedNames = names.stream().sorted((e1, e2) -> e2.compareTo(e1)).collect(Collectors.toList());
collect(container):
- Used to return the result of the intermediate operations performed on the stream.List<Integer> numbers = Arrays.asList(2,3,4,5,3);
Set<Integer> squareSet = numbers.stream().map(e -> e*e).collect(Collectors.toSet());
forEach(operations):
- Used to iterate through every element of the stream.List<Integer> numbers = Arrays.asList(2,3,4,5);
numbers.stream().forEach(e -> System.out.print(e + " "));
reduce(binaryOperator):
- used to reduce the elements of a stream to a single value, it takes a BinaryOperator as a parameter.List<Integer> numbers = Arrays.asList(2,3,4,5);
int evenSum = numbers.stream().filter(e -> e%2==0).reduce(0, (sum, i) -> sum+i);
// Here sum variable is assigned 0 as the initial value and i is added to it .
Example:
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamCreationAndOperations {
public static void main(String[] args){
List<Integer> numbers = Arrays.asList(2, 3, 4, 5, 2);
List<String> names = Arrays.asList("Reflection", "Collection", "Stream");
// Map each element to another value using :=> map(lambdaMapper)
List<Integer> squares = numbers.stream().map(e -> e*e).collect(Collectors.toList());
System.out.println(squares);
// Filter elements using :=> filter(lambdaFilterer)
List<String> filteredNames = names.stream().filter(e -> e.startsWith("S"))
.collect(Collectors.toList());
System.out.println(filteredNames);
// Sort elements using :=> sorted(lambdaComparator)
List<String> sortedNames = names.stream().sorted((e1, e2) -> e2.compareTo(e1))
.collect(Collectors.toList());
System.out.println(sortedNames);
// Collect from stream using :=> collect(container)
Set<Integer> squareSet = numbers.stream().map(e -> e*e).collect(Collectors.toSet());
System.out.println(squareSet);
// Iterate over each element in stream using :=> forEach(operations)
numbers.stream().forEach(e -> System.out.print(e + " "));
System.out.println();
// Reduce the stream to a single entity using :=> reduce(binaryOperator)
int evenSum = numbers.stream().filter(e -> e%2==0).reduce(0, (sum, i) -> sum+i);
System.out.println(evenSum);
}
}
Output:
[4, 9, 16, 25, 4]
[Stream]
[Stream, Reflection, Collection]
[16, 4, 9, 25]
2 3 4 5 2
8