List manipulation by using Stream in Java
For modern Java, there are a number of features that greatly enhance the expressiveness of the language. These features include Generics and Lambda Expressions. And there is another one: the Stream API. Stream is designed with the integration of Lambda Expressions that can reduce the verbosity of the language. This API has been an important feature of the language since version 8.
A Stream is a sequence of objects. Like any generic interfaces, Stream<T> is declared as a variable where T can be Integer, Double, String, YourClass and so on. A stream moves data by filtering, sorting, and other type of manipulation. It doesn't provide a storage or modify the data source. Manipulating a stream will result in the creation of a new stream. Below are some examples to show how to use the Stream API to manipulate a list.
In the above demo, there is function that is less easy to understand. Pay attention to this expression:
The Stream interface has a function called "reduce" that is declared as:
T reduce(T identity, BinaryOperator<T> accumulator)
The reduce function performs a reduction of the stream elements by using an identity value and an accumulator function. In this example, the identity value is 0 which is the initial value of the calculation; and the accumulator function is (answer, element) -> answer + element that performs the sum of the filtered stream elements when applied repetitively within the loop internally.
Generically, the accumulator function is defined as this expression:
For the CalmStreamDemo above, here is the output:
My list: [8, 10, 12, 3, 5, 7]
Min number: 3
Max number: 12
Sorted stream: 3 5 7 8 10 12
Even numbers: 8 10 12
Even numbers greater than 8: 10 12
Squared list: [64, 100, 144, 9, 25, 49]
Sorted squared List: [9, 25, 49, 64, 100, 144]
Squared list by using forEach: 64 100 144 9 25 49
Sum of the even numbers: 30
A Stream is a sequence of objects. Like any generic interfaces, Stream<T> is declared as a variable where T can be Integer, Double, String, YourClass and so on. A stream moves data by filtering, sorting, and other type of manipulation. It doesn't provide a storage or modify the data source. Manipulating a stream will result in the creation of a new stream. Below are some examples to show how to use the Stream API to manipulate a list.
import java.util.*; import java.util.stream.*; class CalmStreamDemo { public static void main (String[] args) { List<Integer> myList = Arrays.asList(8, 10, 12, 3, 5, 7); System.out.println("My list: " + myList); Stream<Integer> myStream = myList.stream(); Optional<Integer> minNum = myStream.min(Integer::compare); if (minNum.isPresent()) System.out.println("Min number: " + minNum.get()); myStream = myList.stream(); // myStream is already consumed above, // it needs to be assigned again Optional<Integer> maxNum = myStream.max(Integer::compare); if (maxNum.isPresent()) System.out.println("Max number: " + maxNum.get()); Stream<Integer> sortedStream = myList.stream().sorted(); System.out.print("Sorted stream: "); sortedStream.forEach ( (n) -> System.out.print(n + " ") ); System.out.println(); Stream<Integer> evenNumStream = myList.stream().sorted() .filter( (n) -> n % 2 == 0 ); System.out.print ("Even numbers: "); evenNumStream.forEach ( (n) -> System.out.print (n + " ")); System.out.println(); Stream<Integer> evenNumGreaterThan8Stream = myList.stream() .filter( (n) -> n % 2 == 0) .filter( (n) -> n > 8); System.out.print ("Even numbers greater than 8: "); evenNumGreaterThan8Stream.forEach ( (n) -> System.out.print(n + " ") ); System.out.println(); List<Integer> squaredList = myList.stream() .map(x->x*x) .collect(Collectors.toList()); System.out.println("Squared list: " + squaredList); List<Integer> sortedSquaredList = myList.stream() .map(x->x*x) .sorted() .collect(Collectors.toList()); System.out.println("Sorted squared List: " + sortedSquaredList); System.out.print("Squared list by using forEach: "); myList.stream().forEach( x -> System.out.print (x*x + " ") ); System.out.println(""); int sumEven = myList.stream() .filter(x->x%2==0) .reduce(0, (answer, element) -> answer + element); System.out.println ("Sum of the even numbers: " + sumEven); } }
In the above demo, there is function that is less easy to understand. Pay attention to this expression:
int sumEven = myList.stream() .filter(x->x%2==0) .reduce(0, (answer, element) -> answer + element);
The Stream interface has a function called "reduce" that is declared as:
T reduce(T identity, BinaryOperator<T> accumulator)
The reduce function performs a reduction of the stream elements by using an identity value and an accumulator function. In this example, the identity value is 0 which is the initial value of the calculation; and the accumulator function is (answer, element) -> answer + element that performs the sum of the filtered stream elements when applied repetitively within the loop internally.
Generically, the accumulator function is defined as this expression:
T answer = identity; for (T element: theStreamOfElements) answer = accumulator.apply (answer, element) return answer;
For the CalmStreamDemo above, here is the output:
My list: [8, 10, 12, 3, 5, 7]
Min number: 3
Max number: 12
Sorted stream: 3 5 7 8 10 12
Even numbers: 8 10 12
Even numbers greater than 8: 10 12
Squared list: [64, 100, 144, 9, 25, 49]
Sorted squared List: [9, 25, 49, 64, 100, 144]
Squared list by using forEach: 64 100 144 9 25 49
Sum of the even numbers: 30
Comments
Post a Comment