Home core java Stream filter examples - Java 8

Stream filter examples – Java 8

In this article we will see what is Stream filter method in Java 8 and how its works in java with different examples.

1. filter() method in Java 8 Stream Interface

The filter() method is an intermediate operation of the Stream interface that allows us to filter elements of a stream that match a given Predicate.

Method Signature :

Stream filter(Predicate<? super T> predicate)

2. Stream filter method examples :

2.1. Stream filter with forEach() example :

Program to print even numbers from list of elements using filter() and forEach() of Stream interface.

public class StreamForEachDemo {
 
    public static void main(String[] args) {
    
        List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        list.stream() // Creating Stream from collection source
        .filter(i -> i%2 == 0) // Intermediate Operation
        .forEach(System.out::println); // Terminal Operation
     
        //Output - 10  12  20  16              
    }
}

2.2. filter() with collect() Example :

- Advertisement -

Program to print numbers those are divisible by 3 from list of elements using filter() and collect() of Stream interface.

public class StreamCollectDemo {
 
    public static void main(String[] args) {
 
        List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        int totalMatched = list.stream() 
        .filter(i -> i%3 == 0)
        .collect(Collectors.counting()).intValue();
     
        System.out.println(totalMatched);
        //15 and 12 are divisible by 3, they satisfy filter condition
        //Output - 2 
    }
}

2.3. filter() with reduce() method example :

Program to print the sum of all elements those are divisible by 3 from list of elements using filter() and reduce() of Stream interface.

public class StreamReduceDemo {
 
    public static void main(String[] args) {
        
         List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
            
            list.stream() 
            .filter(i -> i%3 == 0)
            .reduce((a,b) -> a+b)
            .ifPresent(System.out::println);
            
            //15 and 12 are divisible by 3, they satisfy filter condition
            //Output - 27 
    }
}

2.4. filter() and allMatch() Example :

Program to print whether all the filtered stream elements are greater than are equals to 10 or not from list of elements using fiter() and allMatch() of Stream interface.

public class StreamAllMatchDemo {
 
    public static void main(String[] args) {
 
        List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        boolean isItemFound = list.stream() 
       .filter(i -> i%2 == 0)
       .allMatch(i -> i>=10);
     
       System.out.println(isItemFound);
       
       //10,12,20,16 satisfy, all of them is >= 10
       //Output - true
    }
}

2.5. Stream filter example with anyMatch() :

Program to print whether any of the filtered stream elements are greater than 20 or not from list of elements using filter() and anyMatch() of Stream interface.

public class StreamAnyMatchDemo {
 
    public static void main(String[] args) {
        
        List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        boolean isItemFound = list.stream() 
       .filter(i -> i%2 == 0)
       .anyMatch(i -> i>20);
     
       System.out.println(isItemFound);
       
       //10,12,20,16 satisfy, but no of them is > 20
       //Output - false
    }
}

2.6. filter() with max() :

Program to print max element if found, returned by stream those elements are divisible by 5 from list of elements using filter() and max() of Stream interface.

public class StreamMaxDemo {
 
    public static void main(String[] args) {
        
    List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        Comparator<Integer> c = (o1,o2) -> o1.compareTo(o2);
        
        list.stream() 
        .filter(i -> i%5 == 0) 
        .max(c)
        .ifPresent(System.out::println);;
        
        //20, 25 satisfy, but 25 is max according to order given by comparator
        //Output: 25
    }
}

2.7. filter() example with findFirst() :

Program to print first element from stream those are divisible by 3 from list of elements using filter() and findFirst() of Stream Interface.

public class StreamFindFirstDemo {
 
    public static void main(String[] args) {
        
        List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
        
        list.stream() 
       .filter(i -> i%3 == 0)
       .findFirst().ifPresent(System.out::println);
      
       //15,12, satisfy, all of them 15 comes first in the stream.
       //Output - 15
    }
}

2.8. filter() example with findAny() :

Program to print whether any of the element of stream found, those stream elements are divisible by 7 from list of elements using filter() and findAny() of Stream interface.

public class StreamFindAnyDemo {
 
    public static void main(String[] args) {
        
         List<Integer> list = Arrays.asList(10,13,5,15,12,20,11,25,16);
            
         boolean itemFound = list.stream() 
         .filter(i -> i%7 == 0)
         .findAny().isPresent();
          
         //no element satisfy filter
         //Output - false
         System.out.println(itemFound);
    }
}

Related Articles :

  1. Stream 8 API
  2. forEach() Examples
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles