Eduardo Robles
Functional Programming

Post Overview: This post summarizes Java Functional Programming.


Table of Contents


Functional Interfaces

  1. 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.
  1. 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().
  1. 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).
  1. 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).
  1. 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).
  1. 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().