Flat Preloader Icon

Java Functional Interfaces

In Java, functional interfaces play a vital role in the implementation of lambda expressions and method references. A functional interface is an interface that contains exactly one abstract method. This characteristic allows it to be used as the target for lambda expressions and method references, enabling functional programming in Java.

Here Are Some Commonly Used Functional Interfaces In Java

Consumer:

  • Represents an operation that accepts a single input argument and returns no result. It is typically used to perform operations such as printing, logging, etc.

Supplier:

  • Represents a supplier of results. It has no input arguments and is used to generate or supply values without taking any input.

Predicate:

  • Represents a predicate (boolean-valued function) of one argument. It is commonly used for filtering or testing elements.

Function:

  • Represents a function that accepts one argument and produces a result. It is often used for data transformation.

UnaryOperator:

  • Represents an operation on a single operand that produces a result of the same type as its operand. It is a special kind of Function where the argument and the result are of the same type.

BinaryOperator:

  • Represents an operation upon two operands of the same type, producing a result of the same type. It is used for operations like addition, multiplication, etc.
Here’s an example that demonstrates the usage of functional interfaces:
				
					import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.Predicate;
import java.util.function.Function;

public class FunctionalInterfaceExample {
    public static void main(String[] args) {
        // Consumer example
        Consumer<String> consumer =
        (s) -> System.out.println(s);
        consumer.accept("Hello, world!");

        // Supplier example
        Supplier<Integer> supplier = () -> 42;
        System.out.println("The answer is: "
        + supplier.get());

        // Predicate example
        Predicate<Integer> predicate =
        (num) -> num > 0;
        System.out.println("Is 10 positive? "
        + predicate.test(10));

        // Function example
        Function<Integer, String> function = 
        (num) -> "The number is: " + num;
        System.out.println(function.apply(100));
    }
}

				
			

By leveraging functional interfaces, Java enables developers to write more concise and expressive code, especially in scenarios where behavior needs to be passed as a parameter. They provide a way to implement functional programming concepts in the Java programming language.

Java Predefined-Functional Interfaces

  • Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.
Interface Description
BiConsumer (T,U) It represents an operation that accepts two input arguments and returns no result.
Consume(T) It represents an operation that accepts a single argument and returns no result.
Function(T,R) It represents a function that accepts one argument and returns a result..
Predicate(T) It represents a predicate (boolean-valued function) of one argument.
BiFunction(T,U,R) It represents a function that accepts two arguments and returns a a result.
BinaryOperator(T) It represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.