Java Stream filter() Example
Tags: java, example
Published: 2021-12-09 11:12:15

The Java Stream _filter()_ is an intermediate operation.


The Java Stream _filter()_ can be used to filter out elements from a Java Stream. 

The filter() method takes a _Predicate_ that is called for each element in the stream. If the element is to be included in the resulting Stream, the Predicate should return _true_. If the element should not be included, the Predicate should return _false_.

## Java Stream filter() Example 1
Here is an example of calling the Java Stream _filter()_ method:
```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Tester {
   public static void main(String[] args){
        List<String> lines = Arrays.asList("java", "c", "python");

        List<String> result = lines.stream()       // convert list to stream
                .filter(line -> !"c".equals(line)) // we dont like c
                .collect(Collectors.toList());     // collect the output and convert streams to a List

        result.forEach(System.out::println);  
    }
}
```
Output:
```
java
python
```

## Java Stream filter() Example 2
In this example, we will create a list of products and we filter products whose price is greater than 25k. We display a list of products using the _forEach()_ method.

Let's first create a Product class:
```java
class Product {
    private int id;
    private String name;
    private float price;
   // getters and setters 
}
public class StreamFilterExample {
    public static void main(String[] args) {

        // using stream API
        List < Product > filteredProducts = getProducts().stream()
            .filter((product) -> product.getPrice() > 25000 f)
            .collect(Collectors.toList());
        filteredProducts.forEach(System.out::println);
    }

    private static List < Product > getProducts() {
        List < Product > productsList = new ArrayList < Product > ();
        productsList.add(new Product(1, "HP Laptop", 25000 f));
        productsList.add(new Product(2, "Dell Laptop", 30000 f));
        productsList.add(new Product(3, "Lenevo Laptop", 28000 f));
        productsList.add(new Product(4, "Sony Laptop", 28000 f));
        productsList.add(new Product(5, "Apple Laptop", 90000 f));
        return productsList;
    }
}
In the above example, we are using the filter() method to filter products whose price is greater than 25k:
       List < Product > filteredProducts = getProducts().stream()
            .filter((product) -> product.getPrice() > 25000 f)
            .collect(Collectors.toList());
```

## Related Java Stream Methods/APIs Examples
<ol><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-filter-example.html" target="_blank">Java Stream filter() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-map-example.html" target="_blank">Java Stream map() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-flatmap-example.html" target="_blank">Java Stream flatMap() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-distinct-example.html" target="_blank">Java Stream distinct() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-limit-example.html" target="_blank">Java Stream limit() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-peek-example.html" target="_blank">Java Stream peek() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-anymatch-example.html" target="_blank">Java Stream anyMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-allmatch-example.html" target="_blank">Java Stream allMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-nonematch-example.html" target="_blank">Java Stream noneMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-collect-example.html" target="_blank">Java Stream collect() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-count-example.html" target="_blank">Java Stream count() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-findany-example.html" target="_blank">Java Stream findAny() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-findfirst-example.html" target="_blank">Java Stream findFirst() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-foreach-example.html" target="_blank">Java Stream forEach() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-min-example.html" target="_blank">Java Stream min() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-max-example.html" target="_blank">Java Stream max() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-reduce-example.html" target="_blank">Java Stream reduce() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-toarray-example.html" target="_blank">Java Stream toArray() Example</a></li></ol>