Show Menu
Cheatography

Java Functional Interfaces Cheat Sheet (DRAFT) by

Most of the functional interfaces

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Types

PREDICATE
takes one (or two) argume­nt(s) and returns a boolean
UNARY OPERATOR
result and the single argument types are the same
BINARY OPERATOR
result and both argument types are the same
FUNCTION
result and one (or two) argume­nt(s) types are different
SUPPLIER
takes no arguments, returns a value
CONSUMER
takes one (or two) arguments and returns no value

Notes

If the interface accepts primitive arguments: prefixed Double, Int, Long, e.g. Double­Con­sumer
If the interface produces a primitive result: prefixed ToDouble, ToInt, ToLong, e.g. ToDoub­leF­unction
If the interface both accepts and produces a primitive: prefixes combined e.g. IntToD­oub­leF­unction
BiConsumer variants that accept an object type and a primitive are prefixed Obj + the primitive, e.g. ObjDou­ble­Con­sumer

Predicate

Predic­ate­<T>
boolean test(T t)
Represents a predicate (boole­an-­valued function) of one argument (reference type)
BiPred­ica­te<­T,U>
boolean test(T t, U u)
Accepts two arguments (reference types)
Double­Pre­dicate
e boolean test(d­ouble value)
Accepts one double­-valued argument
IntPre­dicate
boolean test(int value)
Accepts one int-valued argument
LongPr­edicate
boolean test(long value)
Accepts one long-v­alued argument

Supplier

Suppli­er<­T>
T get()
Represents a supplier of results (reference type)
Double­Sup­plier
double getAsD­ouble()
A supplier of double­-valued results
IntSup­plier
int getAsInt()
A supplier of int-valued results
LongSu­pplier
long getAsL­ong()
A supplier of long-v­alued results
Boolea­nSu­pplier
boolean getAsB­ool­ean()
A supplier of boolea­n-v­alued results

Consumer

Consum­er<­T>
void accept(T t)
Represents an operation that accepts a single (reference type) input argument and returns no result
Double­Con­sumer
void accept­(double value)
Accepts a single double­-valued argument and returns no result
IntCon­sumer
void accept(int value)
Accepts a single int-valued argument and returns no result
LongCo­nsumer
void accept­(long value)
Accepts a single long-v­alued argument and returns no result
BiCons­ume­r<T­,U>
void accept(T t, U u)
Represents an operation that accepts two (reference type) input arguments and returns no result
ObjDou­ble­Con­sum­er<­T>
void accept(T t, double value)
Accepts an object­-valued and a double­-valued argument, and returns no result
ObjInt­Con­sum­er<­T>
void accept(T t, int value)
Accepts an object­-valued and an int-valued argument, and returns no result
ObjLon­gCo­nsu­mer­<T>
void accept(T t, long value)
Accepts an object­-valued and a long-v­alued argument, and returns no result

Binary Operator

Binary­Ope­rat­or<­T>
T apply(T t, T u)
Represents an operation upon two operands of the same type, producing a result of the same type as the operands (reference type)
Double­Bin­ary­Ope­rator
double applyA­sDo­ubl­e(d­ouble left, double right)
Accepts two double­-valued operands and produces a double­-valued result
IntBin­ary­Ope­rator
int applyA­sIn­t(int left, int right)
Accepts two int-valued operands and produces an int-valued result
LongBi­nar­yOp­erator
long applyA­sLo­ng(long left, long right)
Accepts two long-v­alued operands and produces a long-v­alued result.

Unary Operator

UnaryO­per­ato­r<T>
T apply(T t)
Represents an operation on a single operand that produces a result of the same type as its operand (reference type)
Double­Una­ryO­perator
double applyA­sDo­ubl­e(d­ouble operand)
Accepts single double­-valued operand and produces a double­-valued result
IntUna­ryO­perator
int applyA­sIn­t(int operand)
Accepts a single int-valued operand and produces an int-valued result
LongUn­ary­Ope­rator
long applyA­sLo­ng(long operand)
Accepts a single long-v­alued operand and produces a long-v­alued result

Function

Functi­on<­T,R>
R apply(T t)
Represents a function that accepts one argument and produces a result (reference type)
Double­Fun­cti­on<­R>
R apply(­double value)
Accepts a double­-valued argument and produces a result
IntFun­cti­on<­R>
R apply(int value)
Accepts an int-valued argument and produces a result
LongFu­nct­ion­<R>
R apply(long value)
Accepts a long-v­alued argument and produces a result
Double­ToI­ntF­unction
int applyA­sIn­t(d­ouble value)
Accepts a double­-valued argument and produces an int-valued result
Double­ToL­ong­Fun­ction
long applyA­sLo­ng(­double value)
Accepts a double­-valued argument and produces a long-v­alued result
IntToD­oub­leF­unction
double applyA­sDo­ubl­e(int value)
Accepts an int-valued argument and produces a double­-valued result
IntToL­ong­Fun­ction
long applyA­sLo­ng(int value)
Accepts an int-valued argument and produces a long-v­alued result
LongTo­Int­Fun­ction
int applyA­sIn­t(long value)
Accepts a long-v­alued argument and produces an int-valued result
LongTo­Dou­ble­Fun­ction
double applyA­sDo­ubl­e(long value)
Accepts a long-v­alued argument and produces a double­-valued result.
ToDoub­leF­unc­tio­n<T>
double applyA­sDo­uble(T value)
Accepts a reference type and produces an int-valued result
ToIntF­unc­tio­n<T>
int applyA­sInt(T value)
Accepts a reference type and produces an int-valued result
ToLong­Fun­cti­on<­T>
long applyA­sLong(T value)
Accepts a reference type and produces a long-v­alued result.
BiFunc­tio­n<T­,U,­R>
R apply(T t, U u)
Represents a function that accepts two arguments and produces a result (reference type)
ToDoub­leB­iFu­nct­ion­<T,­U>
double applyA­sDo­uble(T t, U u)
Accepts two reference type arguments and produces a double­-valued result
ToIntB­iFu­nct­ion­<T,­U>
int applyA­sInt(T t, U u)
Accepts two reference type arguments and produces an int-valued result
ToLong­BiF­unc­tio­n<T­,U>
long applyA­sLong(T t, U u)
Accepts two reference type arguments and produces a long-v­alued result

Java Functional Interface

The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.

Custom Interface

@FunctionalInterface
interface CustomFunctionalInterface {
   void display();
}