Java Stream How to - Create Functional Interface with default method








Question

We would like to know how to create Functional Interface with default method.

Answer

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*from   w  w  w .  j ava2s .  c om*/
public class Main {

  public static void main(String[] args) {
    List<Integer> l = Arrays.asList(1, 3, 2, 4);
    System.out.println(filterInt(l, i -> i >= 3));
  }

  public static List<Integer> filterInt(List<Integer> input, IntValidator filter) {
    List<Integer> l = new ArrayList<Integer>();
    for (Integer i : input) {
      if (filter.validate(i)) {
        l.add(i);
      }
    }
    return l;
  }
}

@FunctionalInterface
interface IntValidator { 
  public boolean validate(int i);

  public default boolean validate3(int i) {
    return i < 3;
  };
}

The code above generates the following result.