Java 8 Stream Filter and Collect Example

In this article we are going to see about Java 8 Stream filter() and collect() example program.

StreamFilter.java

package com.ehowtonow.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StreamFilter {

 public static void main(String[] args) {
  List batsmanRanking = new ArrayList<>();
  batsmanRanking.add("Virat Kohli");
  batsmanRanking.add("AB de Villiers");
  batsmanRanking.add("David Warner");
  batsmanRanking.add("Joe Root");
  batsmanRanking.add("Kane Williamson");
  batsmanRanking.add("Quinton de Kock");
  batsmanRanking.add("Faf du Plessis");
  batsmanRanking.add("Babar Azam");
  batsmanRanking.add("Martin Guptill");
  batsmanRanking.add("Shikhar Dhawan");

  System.out.println("Java 7 forEach : ICC One day Batsman Ranking");

  // Java 7 for each
  for (String batsman : batsmanRanking) {
   System.out.println("Name : " + batsman);

  }
  
  // Java 7 for each
  List superBatsman = new ArrayList<>();
  for (String batsman : batsmanRanking) {
   if ("AB de Villiers".equals(batsman)) {
    superBatsman.add(batsman);
   }

  }

  for (String batsman : superBatsman) {
   System.out.println("Super Batsman : " + batsman);

  }
  
  System.out.println("Java 8 Stream Filter & Collect");
   List java8SuperBatsman = batsmanRanking.stream()                // convert list to stream
                 .filter(batsman -> "AB de Villiers".equals(batsman))     // equals check AB de Villiers
                 .collect(Collectors.toList());              // collect the output and convert streams to a List

   java8SuperBatsman.forEach(System.out::println);    
   
   System.out.println("Java 8 Stream Filter & Collect");
   List topBatsmans = batsmanRanking.stream()                // convert list to stream
                 .filter(batsman -> ! "AB de Villiers".equals(batsman))     // not equals check AB de Villiers
                 .collect(Collectors.toList());              // collect the output and convert streams to a List

   topBatsmans.forEach(System.out::println);   
 }

}

Output
Java 7 forEach : ICC One day Batsman Ranking
Name : Virat Kohli
Name : AB de Villiers
Name : David Warner
Name : Joe Root
Name : Kane Williamson
Name : Quinton de Kock
Name : Faf du Plessis
Name : Babar Azam
Name : Martin Guptill
Name : Shikhar Dhawan
Super Batsman : AB de Villiers
Java 8 Stream Filter & Collect
AB de Villiers
Java 8 Stream Filter & Collect
Virat Kohli
David Warner
Joe Root
Kane Williamson
Quinton de Kock
Faf du Plessis
Babar Azam
Martin Guptill
Shikhar Dhawan


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java – ObjDoubleConsumer Interface
  2. Java – Stream.Builder Interface
  3. Java – ToLongFunction Interface
  4. Java 8 Stream Collectors groupingBy with count and sum Example
  5. Java – BiFunction Interface
  6. Java – IntConsumer Interface
  7. Java – BiConsumer Interface
  8. Java – DoubleStream Interface
  9. Java – Predicate Interface
  10. Java – Consumer Interface
  11. Java 8 convert List to Map example
  12. Java – UnaryOperator Interface
  13. Java – ToDoubleBiFunction Interface
  14. Lambda expressions are allowed only at source level 1.8 or above
  15. Java – IntStream Interface

Be the first to comment

Leave a Reply