Learn Java
import com.learnjava.methods.FunctionalInterfaces;

Functional Interfaces & Method References in Java

@Concept

A functional interface is any interface with exactly one abstract method — that single-method constraint is precisely what makes it possible for a lambda expression to implement it, since the lambda's body has an unambiguous single method to become. Java's standard library ships a set of common, ready-made functional interfaces in java.util.function so most everyday needs don't require defining a custom one: Function<T,R> takes a T and returns an R, Predicate<T> takes a T and returns a boolean, Consumer<T> takes a T and returns nothing, and Supplier<T> takes nothing and returns a T.

A method reference, written as ClassName::methodName or object::methodName, is an even shorter alternative to a lambda whenever the lambda would do nothing but call one existing method directly. String::toUpperCase used as a Function<String,String> is exactly equivalent to writing s -> s.toUpperCase(), just without repeating a parameter name that isn't adding any information.

@HowItWorks
1

Exactly one abstract method

That single-method rule is what qualifies an interface as functional and lets a lambda implement it.

2

Common built-in functional interfaces

Function<T,R>, Predicate<T>, Consumer<T>, and Supplier<T> cover most everyday needs without a custom interface.

3

The @FunctionalInterface annotation

Optional, but documents intent and makes the compiler flag it as an error if a second abstract method is ever added by mistake.

4

Method references as shorthand

ClassName::methodName or object::methodName replaces a lambda that would just call one existing method with nothing else added.

5

Four kinds of method references

A static method, an instance method on a particular object, an instance method on an arbitrary object of a type, and a constructor can each be referenced this way.

@Example
import java.util.function.Function;
 
public class FunctionalDemo {
public static void main(String[] args) {
Function<String, String> shout = String::toUpperCase;
System.out.println(shout.apply("hello"));
}
}
Output
HELLO
1shout is declared as a Function<String, String>, a functional interface taking a String in and returning a String out.
2String::toUpperCase is a method reference standing in for the lambda s -> s.toUpperCase(), naming the exact method to call directly.
3shout.apply("hello") invokes that referenced method on the given argument, "hello".
4The single println call prints the returned result, "HELLO", as one line.
@UseCases
Passing existing methods directly into APIs that expect a functional interface, without wrapping them in a lambda
Choosing between Function, Predicate, Consumer, and Supplier based on what a piece of logic takes in and returns
Writing a custom functional interface for a specific use case a built-in one doesn't quite cover
Shortening a lambda that only calls one existing method into an equivalent method reference

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.