Nearby lessons

91 of 125

Java - Functional Interfaces

📌 What You Will Learn
  • Lambda expressions — the big change
  • Functional interfaces
  • Default and static methods in interfaces
  • The four workhorses: Predicate, Function, Consumer, Supplier
  • Method references (::)
  • The Stream API for processing collections
  • Optional — safe null handling
  • The new Date and Time API

Functional Interfaces is a core concept of the Java language. This lesson explains Functional Interfaces, The Four Functional Interfaces in java.util.function and Built-in Functional Interfaces — The Full List with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Functional Interfaces

A functional interface is an interface with exactly one abstract method. Because it has only one method, a lambda can implement it directly. We mark it with @FunctionalInterface.

In simple words: A functional interface has exactly one abstract method. Because there is only one method, Java knows that a lambda body belongs to it — that is what lets a lambda implement an interface directly.
Example01
JCode Cell
1@FunctionalInterface
2interface Greeting {
3 void sayHello(String name); // only ONE abstract method
4}
5 
6class Demo {
7 public static void main(String[] args) {
8 Greeting g = name -> System.out.println("Hello " + name);
9 g.sayHello("Rahul");
10 }
11}
Output
Hello Rahul

The Four Functional Interfaces in java.util.function

Java 8 provides ready-made functional interfaces. These four cover almost everything:

InterfaceWhat it doesMethodExample
Predicate<T>Tests a condition -> true/falseboolean test(T)x -> x > 0 (is it positive?)
Function<T,R>Converts T into RR apply(T)s -> s.length() (String -> int)
Consumer<T>Does something, returns nothingvoid accept(T)x -> System.out.println(x)
Supplier<T>Supplies/gives a valueT get()() -> Math.random()
Example02
JCode Cell
1import java.util.function.*;
2 
3class FunctionDemo {
4 public static void main(String[] args) {
5 Predicate<Integer> isPositive = n -> n > 0;
6 System.out.println("5 positive? " + isPositive.test(5));
7 System.out.println("-3 positive? " + isPositive.test(-3));
8 
9 Function<String, Integer> len = s -> s.length();
10 System.out.println("Length of Java: " + len.apply("Java"));
11 
12 Consumer<String> print = s -> System.out.println("Msg: " + s);
13 print.accept("Hello");
14 
15 Supplier<Double> random = () -> Math.random();
16 System.out.println("Random: " + random.get());
17 }
18}
Output
5 positive? true -3 positive? false Length of Java: 4 Msg: Hello Random: 0.4123456789

Built-in Functional Interfaces — The Full List

Java already had several interfaces with exactly one abstract method — these are all functional interfaces you can use with lambdas:

InterfaceSingle abstract methodUsed for
Runnablerun()Threads (already seen in Chapter 11)
Comparable<T>compareTo(T)Natural sorting (Chapter 15)
ActionListeneractionPerformed(ActionEvent)GUI button clicks (Chapter 17)
Callable<T>call()Returning a value from a task
Comparator<T>compare(T, T)Custom sorting

Plus the java.util.function package has Bi-variants for two parameters — BiFunction<T,U,R>, BiConsumer<T,U>, BiPredicate<T,U> — and primitive versions like IntFunction, LongSupplier for performance.

Example03
JCode Cell
1// BiFunction - two inputs, one output
2BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
3System.out.println(add.apply(10, 20)); // 30
4 
5// Runnable as a lambda (like Chapter 11, but shorter)
6Runnable job = () -> System.out.println("Lambda thread");
7new Thread(job).start();
Output
30 Lambda thread
📝 Key Takeaways
  • Lambda = a function without a name; (params) -> logic.
  • Functional interface = one abstract method, so a lambda can implement it.
  • Interfaces now have default and static methods with bodies.
  • Predicate (test), Function (apply), Consumer (accept), Supplier (get) are the four core functional interfaces.
  • Method references (::) are shorthand for lambdas.
  • Streams process collections with filter/map/sorted/collect pipelines.
  • Optional prevents NullPointerException with orElse/isPresent.
  • java.time (LocalDate, LocalDateTime) replaces the old Date/Calendar cleanly.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1