Functional Interface:
A functional interface is an interface that contains exactly one
abstract method. It can have multiple default or static methods.
Used to represent lambda expressions or method references.
Marked optionally with @FunctionalInterface annotation.
Example:
Java:
@FunctionalInterface
interface MyInterface {
void doSomething();
}
1. Predicate
Definition:
Represents a boolean-valued function of one argument.
Method Signature:
Java:
boolean test(T t);
Internal Working:
Takes one input of type T
Returns a boolean (true or false)
Commonly used for filtering or conditions
Code Example:
Java:
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<String> isLongName = name -> name.length() > 5;
System.out.println(isLongName.test("Alex")); // false
System.out.println(isLongName.test("Jonathan")); // true
}
}
Analogy:
A security guard checks if a person meets a condition (has ID). Allows or
denies entry based on that.
Use Cases:
Input validation
Filtering lists or streams
Conditional logic in lambda expressions
2. Function
Definition:
Takes one input and returns a result.
Method Signature:
Java:
R apply(T t);
Internal Working:
Accepts an input of type T
Returns output of type R
Can be chained using andThen() or compose()
Code Example:
Java:
import java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
Function<String, Integer> lengthFunc = str -> str.length();
System.out.println(lengthFunc.apply("Banana")); // 6
}
}
Analogy:
A juicer takes a fruit and gives out juice. It transforms one thing into
another.
Use Cases:
Type conversions (e.g., String to Integer)
Mapping values in a stream
Creating reusable transformation logic
3. Consumer
Definition:
Performs an action on an input without returning a result.
Method Signature:
Java:
void accept(T t);
Internal Working:
Accepts a single input of type T
Does not return anything
Used for side effects (like printing or saving)
Code Example:
Java:
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> greeter = name -> System.out.println("Hello, " +
name);
greeter.accept("Alice"); // Output: Hello, Alice
}
}
Analogy:
A speaker takes your voice and plays it out. It acts but does not give
back anything.
Use Cases:
Logging and displaying output
Applying side effects in stream operations
UI event handling
4. Supplier
Definition:
Supplies a value with no input.
Method Signature:
Java:
T get();
Internal Working:
No input
Returns a value of type T
Often used for lazy loading or object creation
Code Example:
Java:
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<Double> randomSupplier = () -> Math.random();
System.out.println(randomSupplier.get());
}
}
Analogy:
A vending machine gives you a snack when you press a button. No input
required.
Use Cases:
Lazy initialization
Generating IDs, timestamps, or random numbers
Providing default values
5. BinaryOperator
Definition:
Takes two same-type inputs and returns one same-type result.
Method Signature:
Java:
T apply(T t1, T t2);
Internal Working:
Accepts two arguments of type T
Returns one result of type T
Sub-interface of BiFunction<T, T, T>
Code Example:
Java:
import java.util.function.BinaryOperator;
public class BinaryOperatorExample {
public static void main(String[] args) {
BinaryOperator<Integer> adder = (a, b) -> a + b;
System.out.println(adder.apply(10, 20)); // 30
}
}
Analogy:
A blender takes two ingredients and blends them into one smoothie.
Use Cases:
Reducing a list (sum, product)
Combining values in data structures
Mathematical calculations with two values
Summary Cheat Sheet Table
Method Common Use
Interface Input Output Analogy
Signature Case
boolean Security
Predicate 1 boolean Validations, filters
test(T) guard
Function R apply(T) 1 1 Juicer Data
transformation,
Method Common Use
Interface Input Output Analogy
Signature Case
mapping
void Logging, UI
Consumer 1 void Speaker
accept(T) actions
Vending Random data,
Supplier T get() 0 1
machine lazy-loading
T apply(T, Math operations,
BinaryOperator 2 1 Blender
T) stream reduce