Post Overview: This post summarizes Java Functional Programming.
Table of Contents
Functional Interfaces
- Callable<V>
- Represent a function that takes no arguments, returns a result and may throw an exception..
- Commonly used with ExecutorService or FutureTask to run computations in another thread and retrieve the result, or any object that needs to perform actions.
- Its functional method is V call() throws Exception.
- Runnable
- Represents a function that takes no arguments and returns no result.
- Used by Thread, ExecutorService, UI framrworks (Swing, JavaFX), FutureTask, or any object that needs to perform actions.
- Its functional method is void run().
- Predicate<T>
- Represents a function that takes one argument and returns a boolean result.
- Often used in filtering, matching, and test conditions.
- Its functional method is boolean test(T t).
- Function<T,R>
- Represents a function that takes one argument and returns a result.
- Used anywhere you need to transform or map a value.
- Its functional method is R apply(T t).
- Consumer<T>
- Represents a function that takes one argument and returns no result.
- Mostly used for side-effects, i.e., actions performed on each element of a collection or stream, logging, printing, updating state, etc.
- Its functional method is void accept(T t).
- Supplier<T>
- Represents a function that takes no arguments and returns a result.
- Used when you want to generate or supply a value on demand.
- Its functional method is T get().