Functional Interfaces for Lambda

Predicate, Function, Consumer, Supplier

6 min read

Functional Interfaces

Functional interfaces have exactly one abstract method and are the foundation of lambda expressions.

Code Examples

Core functional interfaces

java
1
2Predicate<Integer> isEven = n -> n % 2 == 0;
3Function<String, Integer> length = s -> s.length();
4Consumer<String> print = s -> System.out.println(s);
5Supplier<Double> random = () -> Math.random();
6          

Use Cases

  • Stream operations
  • Event handling

Common Mistakes to Avoid

  • Confusing Predicate and Function
  • Writing custom interfaces unnecessarily