Standard Built-In Interfaces
Details about standard built-in functional interfaces in Java.
Java java.util.function package provides a set of built-in functional interfaces. Let's see some of the most commonly used.
Function<T, R>
Description: This interface represents a function that takes one argument of type
Tand returns a result of typeR.Abstract method:
R apply(T t)Common Use Cases:
Data transformation: Convert one type of data to another (e.g., String to Integer, Integer to String).
Calculations: Perform operations on a single value (e.g., squaring a number, finding the absolute value).
Example:
Function<String, Integer> stringToInt = str -> Integer.parseInt(str);
String numberString = "10";
int number = stringToInt.apply(numberString);
// Value of number will be 10Predicate<T>
Description: This interface represents a function that takes one argument of type
Tand returns a boolean value (true or false).Abstract method:
boolean test(T t)Common Use Cases:
Filtering: Select elements from a collection based on a condition.
Validation: Check if an object meets specific criteria.
Example:
Consumer<T>
Description: This interface represents a function that takes one argument of type
Tbut doesn't return a value (void).Abstract method:
void accept(T t)Common Use Cases:
Performing side effects: Printing to console, logging data, modifying state.
Iterating over collections and performing actions on each element.
Example:
Supplier<T>
Description: This interface represents a function that doesn't take any arguments but returns a value of type
T.Abstract method:
T get()Common Use Cases:
Generating values: Creating new objects, initializing variables with random values.
Lazy evaluation: Delaying the creation of a value until it's actually needed.
Example:
BiFunction<T, U, R>
Description: Represents a function that takes two arguments of type
TandUand returns a result of typeR.Abstract method:
R apply(T t, U u)Common Use Cases:
Combining values from two sources: Calculate the sum of two numbers, concatenate two strings.
Performing operations on pairs of elements: Comparing two objects based on multiple criteria.
Example:
BiConsumer<T, U>
Description: Represents a function that takes two arguments of type
TandUbut doesn't return a value (void).Abstract method:
void accept(T t, U u)Common Use Cases:
Processing pairs of elements: Modifying two objects based on their relationship.
Logging: Logging two values together.
Example:
BiPredicate<T, U>
Description: Represents a function that takes two arguments of type
TandUand returns a boolean value (true or false).Abstract method:
boolean test(T t, U u)Common Use Cases:
Conditional logic involving two arguments: Checking if two elements are equal, comparing two objects based on multiple criteria.
Filtering based on two conditions: Selecting elements from a collection that satisfy conditions on both attributes.
Example:
BinaryOperator<T>
Description: Represents an operation upon two operands of the same type, producing a result of the same type. It is a specialization of
BiFunction<T, T, T>, where the two arguments and the result are of the same type.Abstract method:
T apply(T t1, T t2)Common Use Cases:
Performing arithmetic operations on two values of the same type (e.g., sum, max, min).
Reducing or combining elements in a collection (e.g., finding the maximum value in a list).
Used in
Stream.reduce()to combine elements using an associative function.
Example:
IntFunction<R>, LongFunction<R>, DoubleFunction<R>
Input type: These interfaces take a primitive data type as an argument:
IntFunction<R>: Takes anintargument.LongFunction<R>: Takes alongargument.DoubleFunction<R>: Takes adoubleargument.
Return type: They return a value of any type (
R).Common Use Cases:
Converting primitive values to objects: These functions are useful when you need to create objects based on primitive data values.
Performing operations with primitive inputs: You can implement custom logic to operate on primitive data types and return objects as results.
Example:
BiFunction<int, U, R>, BiFunction<long, U, R>, BiFunction<double, U, R>
BiFunction<int, U, R>: Takes two arguments, anintand one of any type (U), and returns a value of typeR.BiFunction<long, U, R>: Takes two arguments, alongand one of any type (U), and returns a value of typeR.BiFunction<double, U, R>: Takes two arguments, adoubleand one of any type (U), and returns a value of typeR.Common Use Cases:
Converting primitive values to objects: These functions are useful when you need to create objects based on primitive data values.
Performing operations with primitive inputs: You can implement custom logic to operate on primitive data types and return objects as results.
Example:
ToIntFunction<T>, ToLongFunction<T>, ToDoubleFunction<T> and BiFunction counterparts
These interfaces are designed for primitive type conversions and are related to the Function interface. They differ in the following ways:
Input type: These interfaces take an argument of any type (
T).Return type: They specialize in returning primitive data types:
ToIntFunction<T>: Returns anint.ToLongFunction<T>: Returns along.ToDoubleFunction<T>: Returns adouble.
BiFunction counterparts:
BiToIntFunction<T, U>: Takes two arguments of any type (T,U) and returns anint.BiToLongFunction<T, U>: Takes two arguments of any type (T,U) and returns along.BiToDoubleFunction<T, U>: Takes two arguments of any type (T,U) and returns adouble.
Common Use Cases:
Primitive stream operations: These functions are commonly used in conjunction with primitive streams (
IntStream,LongStream,DoubleStream) to map elements to primitive data types.Custom conversion logic: You can implement custom logic to convert objects of any type to specific primitive data types.
Example:
Last updated