What is Stream in java?

Steam in Java 8 is present in java.util.stream package, this package also contains classes, interfaces, and enums.

They perform operations on the collection objects to filter, print, or collect the data.

We have different methods for the stream in java. We will mainly see the stream interface and how we can use it to iterate over the collection objects or to filter the same.

Note that Stream is not a data structure, it takes input from the Collections, Arrays, or I/O channels.

Stream doesn’t change the original data structure.

Let’s see some practical methods of Stream interface.

  1. filter(Predicate condition): This accepts the predicates and filters the records based on a given condition. As shown below image we can filter the list for even numbers.
  1. collect(Collector collector): This is used to collect the records in another collection object. As shown in the above image we can collect the filtered records for the even number and save them into the ‘even’ list.
  2. sorted(): This is used to sort the list. By default, it will sort in natural sorting order. We can also add a comparator to sort according to the requirements as shown in the below image we have both natural sorting order and user-defined order using Comparator.
  1. allMatch(Predicate condition): As shown in the example below it will accept the predicate and will return the boolean value based on the condition.
  1. anyMatch(Predicate condition): This will accept a predicate and based on the condition added it will check if any value is matching that condition. This method will return true if any match is found else will return false.
  1. noneMatch(Predicate condition): This will accept a predicate and check if no value is present for that condition added. It will return true if no match is found else will return false.
  1. max(Comparator comparator): This method accepts Comparator and returns the maximum value based on the condition.
  1. min(Comparator comparator): This method accepts Comparator and returns the minimum value based on the condition.
  1. map(Function mapper): This will accept a function, and based on the operation done it will return the new steam of the list with updated values. As shown in the following example we can add 100 to each number and create a new list.
  1. groupingBy(Function function): we can group by using a collect method which takes the Collectors class. 
Java Code
import java.util.concurrent.locks.ReentrantLock;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

class Test {
public static void main(String args[]) {

List<Integer> list = (List<Integer>) Arrays.asList(11, 32, 45, 12, 34, 3, 87, 14, 99, 57, 26);

// ——————- filter ——————-
List<Integer> even = list.stream()
.filter(a -> a % 2 == 0)
.collect(Collectors.toList());

System.out.println(“list of even number: ” + even);

// ——————- sorted ——————-
// Natural sorting order
List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList());
System.out.println(“sorted list: ” + sortedList);

// Sorting in descending order using Comparator
List<Integer> sortedDescList = list.stream().sorted(new Comparator<Integer>() {
public int compare(Integer v1, Integer v2) {
if(v1 < v2) {
return 1;
}
return -1;
}})
.collect(Collectors.toList());
System.out.println(“sorted list desc order: ” + sortedDescList);

// ——————- allMatch ——————-
boolean allMatch1 = list.stream().allMatch(a -> a > 2);
boolean allMatch2 = list.stream().allMatch(a -> a > 10);

System.out.println(“All > 2 : ” + allMatch1); // true
System.out.println(“All > 10 : ” + allMatch2); // false

// ——————- anyMatch ——————-
boolean anyMatch1 = list.stream().anyMatch(a -> a > 10 && a % 3 == 0);
boolean anyMatch2 = list.stream().anyMatch(a -> a > 10 && a % 10 == 0);

System.out.println(“Any value > 10 and divisible by 3 : ” + anyMatch1); // true
System.out.println(“Any value > 10 and divisible by 10 : ” + anyMatch2); // false

// ——————- noneMatch ——————-
boolean noneMatch1 = list.stream().noneMatch(a -> a > 10 && a % 3 == 0);
boolean noneMatch2 = list.stream().noneMatch(a -> a > 10 && a % 10 == 0);

System.out.println(“No Match > 10 and divisible by 3 : ” + noneMatch1); // false
System.out.println(“No Match > 10 and divisible by 10 : ” + noneMatch2); // true

// ——————- max ——————-
Optional<Integer> max = list.stream().max(Integer::compare);
System.out.println(“Max value: ” + max.get());

// ——————- min ——————-
Optional<Integer> min = list.stream().min(Integer::compare);
System.out.println(“Min value: ” + min.get());

// ——————- map ——————-
List<Integer> mapList = list.stream().map(a -> a + 100).collect(Collectors.toList());
System.out.println(“mapList after adding 100 in each ” + mapList);

// ——————- group ——————-
Map<Object, List<Integer>> groupList = list.stream()
.collect(Collectors.groupingBy(a -> a % 2 == 0));
System.out.println(“grouping all the even and odds ” + groupList);
}
}
Output:
list of even number: [32, 12, 34, 14, 26]
sorted list: [3, 11, 12, 14, 26, 32, 34, 45, 57, 87, 99]
sorted list desc order: [99, 87, 57, 45, 34, 32, 26, 14, 12, 11, 3]
All > 2 : true
All > 10 : false
Any value > 10 and divisible by 3 : true
Any value > 10 and divisible by 10 : false
No Match > 10 and divisible by 3 : false
No Match > 10 and divisible by 10 : true
Max value: 99
Min value: 3
mapList after adding 100 in each [111, 132, 145, 112, 134, 103, 187, 114, 199, 157, 126]
grouping all the even and odds {false=[11, 45, 3, 87, 99, 57], true=[32, 12, 34, 14, 26]}

-A blog by Shwetali Khambe

Related Posts