Skip to content

Chapter 29 of 37

Functional Interfaces

Compose Predicate, Function, Consumer, Supplier, and specialized interfaces.

34 minutes 10 quick checksBy Subha Prasad
Lesson 29 of 37Course navigation

Lesson content

Read, practise, then check your understanding

A functional interface has one abstract method; @FunctionalInterface asks the compiler to enforce that shape. Object methods and defaults do not count against it.

Standard vocabulary

Predicate<String> present = text -> text != null;
Predicate<String> useful = present.and(text -> !text.isBlank());
Function<String, Integer> length = String::length;
Supplier<Instant> clock = Instant::now;
Consumer<String> output = System.out::println;

Use primitive specializations such as IntFunction to avoid boxing in hot paths. UnaryOperator and BinaryOperator preserve a type. Prefer standard interfaces for interoperability; create a domain-specific one when its name, checked-exception contract, or extra semantics materially clarify the API.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes functional interface?
Which Java term matches this description: An interface with one abstract method.
Which statement best describes Predicate?
Which Java term matches this description: A function from a value to boolean.
Which statement best describes Function?
Which Java term matches this description: A function transforming one value into another.
Which statement best describes Consumer?
Which Java term matches this description: An operation accepting a value without returning a result.
Which statement best describes Supplier?
Which Java term matches this description: An operation producing a value without an input.

0 of 10 checks passed

Your progress is saved on this device.

Functional Interfaces | Java Lesson | Subha Prasad