Java Stream How to - Use Predicate to filter Integer List








Question

We would like to know how to use Predicate to filter Integer List.

Answer

//from   ww w. j  av a  2s  .c  o m
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Main {
   public static void main(String[] args) {
      List<Integer> numbers = Arrays.asList(1, 7, 3, 11);
      List<String> names = Arrays.asList("John", "Jane", "Max", "Katherine");

      List<Integer> filteredNumbers = filterIt(numbers, x -> x > 5);
      System.out.println("filteredNumbers = " + filteredNumbers);

      List<String> filteredNames = filterIt(names, x -> x.length() > 3);
      System.out.println("filteredNames = " + filteredNames);
   }

   public static <T> List<T> filterIt(List<T> candidates, Predicate<T> condition) {
      List<T> filteredList = new ArrayList<>();

      for(T candidate : candidates) {
         if (condition.test(candidate)) {
            filteredList.add(candidate);
         }
      }

      return filteredList;
   }
}

The code above generates the following result.