Flat Preloader Icon

Java Stream Filter

In Java, the filter method of the Stream API is used to select elements from a stream based on a specified condition. It takes a Predicate as an argument and returns a new stream that consists of elements that satisfy the given predicate. The original stream remains unchanged.

Here Is The General Syntax For Using The Filter Method

				
					Stream<T> filter(Predicate<? super T> predicate)

				
			
  • Here, T represents the type of the elements in the stream, and the Predicate is a functional interface that takes an argument of type T and returns a boolean value.
Here’s an example that demonstrates how to use the filter method in Java:
				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamFilterExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList
        ("John", "Jane", "Adam", "Eva", "Tom", "Emily");

        // Filter names starting with "J"
        List<String> filteredNames = names.stream()
                .filter(name -> name.startsWith("J"))
                .collect(Collectors.toList());

        // Print the filtered names
        System.out.println
        ("Names starting with 'J': " + filteredNames);
    }
}

				
			
  • In this example, we have a list of names, and we use the filter method to select only those names that start with the letter “J”. The resulting stream is collected into a list using the collect method.
The output will be:
				
					Names starting with 'J': [John, Jane]

				
			
  • The filter method is often used in combination with other stream operations, such as map, sorted, and forEach, to perform more complex data processing tasks on collections. It allows you to efficiently select and process elements based on specific criteria, making your code more concise and readable.

Java Stream Filter() Example

  • In the following example, we are fetching filtered data as a list.
				
					import java.util.*;  
import java.util.stream.Collectors;  
class Product{  
    int id;  
    String name;  
    float price;  
    public Product(int id, String name, float price) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
public class JavaStreamExample {  
    public static void main(String[] args) {  
        List<Product> productsList =
        new ArrayList<Product>();  
        //Adding Products  
        productsList.add(new Product
        (1,"HP Laptop",25000f));  
        productsList.add(new Product
        (2,"Dell Laptop",30000f));  
        productsList.add(new Product
        (3,"Lenevo Laptop",28000f));  
        productsList.add(new Product
        (4,"Sony Laptop",28000f));  
        productsList.add(new Product
        (5,"Apple Laptop",90000f));  
        List<Float> pricesList =  productsList.stream()  
                    .filter(p ->p.price> 30000)   
                    // filtering price  
                    .map(pm ->pm.price)          
                    // fetching price  
                    .collect(Collectors.toList());  
        System.out.println(pricesList);  
    }  
}  
				
			

Output:

				
					[90000.0]