Java 8 – Using Functional Interface2 min read

“Interface that exhibits a single functionality is called as Functional Interface. This is Oracle’s way of in-cooperating functional programming into Java.

@FunctionalInterface annotation coupled with a Single Abstract Method marks an Interface as Functional Interface.

We can also have default and static methods in our functional Interfaces.

@FunctionalInterface
interface FunctionalInterfaceEg{
	
    //Mandatory Abstract method
	public void someMethod();

    //Optional Default method
    public default void displayText(String text) {
        System.out.println(text);
    }

    //Optional Static method
    public static void Calculate(int one, int two){
        System.out.println(one*two);
    }
}

1. Lambda Implementation of a Functional Interface

Lambda expressions can be used to implement Functional Interfaces. Here is an example, that implements above defined interface.

public class FunctionalInterfaceImpl {

    public static void main(String[] args) {

        FunctionalInterfaceEg lambda = () -> {
            System.out.println("Executed !!");
        };
        lambda.someMethod();
    }
}

In line 5 & 6, we have provided implementation for the defined Interface method.

2. Predicate

In generic terms, Predicate means to require something as a condition of something else.
The method signature as defined in java.util.Objects.Predicate is:

Predicate Interface

Predicates are helpful in defining filter conditions for your buisness logic, we just need to extend the predicate interface and implement the test method.

-> Let’s go through an example to understand its usage:

class SomePredicate<E> implements Predicate<Integer> {
    @Override
    public boolean test(Integer someNumber) {
        return someNumber % 2 == 0;
    }
}

.
.
List<Integer> listOfNumbers = new ArrayList<>(Arrays.asList(
                              1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
        long size = listOfNumbers
                    .stream()
                    .filter(somePredicate)
                    .count();
        System.out.println("size :" + size);

On line 14, we have called our predicate by just creating a new instance of the SomePredicate class.
Filter logic, if complex and lengthy can be put inside the predicate and can be called any ever it’s needed.

3. Supplier<T>

It is yet another kind of functional interface that represents a function, that supplies any required value.

Example:

Supplier<Integer> supplier = () -> (int) (Math.random() * 1000D);
//To get value from Supplier
System.out.println(supplier.get());

4. Consumer<T>

The Consumer is opposite to suppliers, as they simply perform an operation on the consumed value, instead of returning some value back.

Example:

Consumer<Integer> consumer = (value) -> System.out.println(value);
//Calling Consumer
consumer.accept(10);

It can be helpful in multiple scenarios like; Generating a file based on the passed input, sending values to some external system e.t.c

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.