Java 8 - Predefined functional interfaces
Java provides several built-in functional interfaces that are used to represent common function types. These interfaces are located in the java.util.function package.
Here are some of the commonly used functional interfaces provided by Java:
Function
The Function interface represents a function that accepts one argument and produces a result. The apply() method of the Function interface is used to perform the function on the input argument.
Here’s an example of how to use the Function interface:
1
2
3
Function<String, Integer> stringLength = str -> str.length();
int length = stringLength.apply("Hello, World!");
In this example, we create a Function that accepts a string and returns its length.
Predicate
The Predicate interface represents a function that accepts one argument and returns a boolean value. The test() method of the Predicate interface is used to perform the function on the input argument and return a boolean value.
Here’s an example of how to use the Predicate interface:
1
2
3
Predicate<String> startsWithA = str -> str.startsWith("A");
boolean result = startsWithA.test("Alice");
In this example, we create a Predicate that tests if a string starts with the letter “A”.
Consumer
The Consumer interface represents an operation that accepts a single input argument and returns no result. The accept() method of the Consumer interface is used to perform the operation on the input argument.
Here’s an example of how to use the Consumer interface:
1
2
3
Consumer<String> printString = str -> System.out.println(str);
printString.accept("Hello, World!");
In this example, we create a Consumer that accepts a string and prints it to the console.
Supplier
The Supplier interface represents a supplier of results. The get() method of the Supplier interface is used to return the result.
Here’s an example of how to use the Supplier interface:
1
2
3
Supplier<String> helloWorld = () -> "Hello, World!";
String result = helloWorld.get();
In this example, we create a Supplier that supplies the string “Hello, World!”.
UnaryOperator
Represents an operation on a single operand that produces a result of the same type as its operand.
Example:
1
2
3
UnaryOperator<Integer> negate = (x) -> -x;
Integer result = negate.apply(1); // result is -1
BinaryOperator
Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
Example:
1
2
3
BinaryOperator<Integer> sum = (x, y) -> x + y;
Integer result = sum.apply(1, 2); // result is 3
BiFunction
BiFunction<T, U, R> - Represents a function that accepts two arguments and produces a result.
Example:
1
2
3
BiFunction<Integer, Integer, Integer> sum = (x, y) -> x + y;
Integer result = sum.apply(1, 2); // result is 3
BiConsumer
BiConsumer<T, U> - Represents an operation that accepts two input arguments and returns no result.
Example:
1
2
3
BiConsumer<String, Integer> stringIntConsumer = (s, i) -> System.out.println(s + i);
stringIntConsumer.accept("The answer is: ", 42); // prints "The answer is: 42"
IntSupplier
IntSupplier - Represents a supplier of int-valued results.
Example:
1
2
IntSupplier intSupplier = () -> 42;
int result = intSupplier.getAsInt(); // result is 42
IntConsumer
IntConsumer - Represents an operation that accepts a single int-valued argument and returns no result.
Example:
1
2
IntConsumer intConsumer = (i) -> System.out.println("The answer is: " + i);
intConsumer.accept(42); // prints "The answer is: 42"
IntFunction
Represents an operation that accepts a single int-valued argument and returns no result.
Example:
1
2
IntConsumer intConsumer = (i) -> System.out.println("The answer is: " + i);
intConsumer.accept(42); // prints "The answer is: 42"
IntPredicate
Represents a predicate (boolean-valued function) of one int-valued argument.
IntUnaryOperator
Represents an operation on a single int-valued operand that produces an int-valued result.
IntBinaryOperator
Represents an operation upon two int-valued operands and producing an int-valued result.
ObjIntConsumer
Represents an operation that accepts an object-valued and an int-valued argument, and returns no result.
ObjLongConsumer
Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
ObjDoubleConsumer
Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
LongSupplier
Represents a supplier of long-valued results.
LongConsumer
Represents an operation that accepts a single long-valued argument and returns no result.
LongFunction
Represents a function that accepts a long-valued argument and produces a result.
LongPredicate
Represents a predicate (boolean-valued function) of one long-valued argument.
LongUnaryOperator
Represents an operation on a single long-valued operand that produces a long-valued result.
LongBinaryOperator
Represents an operation upon two long-valued operands and producing a long-valued result.
DoubleSupplier
Represents a supplier of double-valued results.
DoubleConsumer
Represents an operation that accepts a single double-valued argument and returns no result.
DoubleFunction
Represents a function that accepts a double-valued argument and produces a result.
DoublePredicate
Represents a predicate (boolean-valued function) of one double-valued argument.
DoubleUnaryOperator
Represents an operation on a single double-valued operand that produces a double-valued result.
DoubleBinaryOperator
Represents an operation upon two double-valued operands and producing a double-valued result.
Conclusion
In this blog, we explored some of the commonly used functional interfaces provided by Java. These functional interfaces provide a convenient way to work with lambda expressions and method references. By mastering the use of functional interfaces, you can write more concise and efficient code that is easier to read and maintain.