Java Stream Filter

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Stream Filter

Java stream provides a method filter() to filter stream elements on the basis of given predicate. Suppose you want to get only even elements of your list then you can do this easily with the help of filter method.

This method takes predicate as an argument and returns a stream of consisting of resulted elements.


Signature

The signature of Stream filter() method is given below:

  1. Stream filter(Predicate predicate)  

Parameter

predicate: It takes Predicate reference as an argument. Predicate is a functional interface. So, you can also pass lambda expression here.

Return

It returns a new stream.

Java Stream filter() example

In the following example, we are fetching and iterating filtered data.

  1. import java.util.*;  
  2. class Product{  
  3.     int id;  
  4.     String name;  
  5.     float price;  
  6.     public Product(int id, String name, float price) {  
  7.         this.id = id;  
  8.         this.name = name;  
  9.         this.price = price;  
  10.     }  
  11. }  
  12. public class JavaStreamExample {  
  13.     public static void main(String[] args) {  
  14.         List<Product> productsList = new ArrayList<Product>();  
  15.         //Adding Products  
  16.         productsList.add(new Product(1,"HP Laptop",25000f));  
  17.         productsList.add(new Product(2,"Dell Laptop",30000f));  
  18.         productsList.add(new Product(3,"Lenevo Laptop",28000f));  
  19.         productsList.add(new Product(4,"Sony Laptop",28000f));  
  20.         productsList.add(new Product(5,"Apple Laptop",90000f));  
  21.         productsList.stream()  
  22.                     .filter(p ->p.price> 30000)   // filtering price  
  23.                     .map(pm ->pm.price)          // fetching price  
  24.                     .forEach(System.out::println);  // iterating price  
  25.     }  
  26. }  

Output:

90000.0

Java Stream filter() example 2

In the following example, we are fetching filtered data as a list.

 
  1. import java.util.*;  
  2. import java.util.stream.Collectors;  
  3. class Product{  
  4.     int id;  
  5.     String name;  
  6.     float price;  
  7.     public Product(int id, String name, float price) {  
  8.         this.id = id;  
  9.         this.name = name;  
  10.         this.price = price;  
  11.     }  
  12. }  
  13. public class JavaStreamExample {  
  14.     public static void main(String[] args) {  
  15.         List<Product> productsList = new ArrayList<Product>();  
  16.         //Adding Products  
  17.         productsList.add(new Product(1,"HP Laptop",25000f));  
  18.         productsList.add(new Product(2,"Dell Laptop",30000f));  
  19.         productsList.add(new Product(3,"Lenevo Laptop",28000f));  
  20.         productsList.add(new Product(4,"Sony Laptop",28000f));  
  21.         productsList.add(new Product(5,"Apple Laptop",90000f));  
  22.         List<Float> pricesList =  productsList.stream()  
  23.                     .filter(p ->p.price> 30000)   // filtering price  
  24.                     .map(pm ->pm.price)          // fetching price  
  25.                     .collect(Collectors.toList());  
  26.         System.out.println(pricesList);  
  27.     }  
  28. }  

Output:

[90000.0]