🎓 A1Learner
🔍
0 of 40 lessons completed

Java Functional Interfaces

A functional interface is an interface with exactly one abstract method. Java supplies four common ones for tests, conversions, actions and supplied values.

Predicate tests a value

Predicate<Integer> holds a test for whole numbers. The test method runs the lambda on each given score.

import java.util.function.Predicate;

public class Main {
  public static void main(String[] args) {
    Predicate<Integer> isPass = score -> score >= 60;
    System.out.println(isPass.test(82));
    System.out.println(isPass.test(45));
  }
}

Function converts a value

Function<String, Integer> converts text into a number. The apply method answers the length of each text.

import java.util.function.Function;

public class Main {
  public static void main(String[] args) {
    Function<String, Integer> textLength = text -> text.length();
    System.out.println(textLength.apply("lambda"));
    System.out.println(textLength.apply("Java"));
  }
}

Consumer acts on a value

Consumer<String> takes a name and prints a greeting. The accept method runs the action and answers nothing.

import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    Consumer<String> greeter = name -> System.out.println("Hello, " + name);
    greeter.accept("Ana");
    greeter.accept("Ben");
  }
}

Supplier supplies a value

Supplier<String> takes no input at all. Each get call runs the lambda and answers its value.

import java.util.function.Supplier;

public class Main {
  public static void main(String[] args) {
    Supplier<String> nextCode = () -> "A-101";
    System.out.println(nextCode.get());
    System.out.println(nextCode.get());
  }
}

Two abstract methods refuse a lambda

Worker declares two abstract methods, start and stop. The compiler refuses the lambda assignment.

public class Main {
  public static void main(String[] args) {
    Worker worker = () -> System.out.println("Working");
    worker.start();
  }
}
interface Worker {
  void start();
  void stop();
}

Example

public class Main {
  public static void main(String[] args) {
    Checker isPass = score -> score >= 60;
    System.out.println(isPass.check(90));
    System.out.println(isPass.check(50));
  }
}
interface Checker {
  boolean check(int score);
}

Key points

  • Predicate<T> tests one value, and its test method answers a boolean.
  • Function<T, R> converts one value, and its apply method answers the result.
  • Consumer<T> consumes one value, and its accept method answers nothing.
  • Supplier<T> supplies one value, and its get method takes no input.
  • An interface written in a program with one abstract method also accepts a lambda.
  • A second abstract method breaks a lambda assignment to that interface.

In this example, Checker is an interface with one abstract method named check. A lambda fills that method with the passing test. The two calls print true for 90 and false for 50.

Note

Each built-in interface names one method, and that method is the lambda target.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which built-in interface supplies a value without taking any input?