In this blog, we will be discussing Java functional interfaces and one specific interface Function interface. In the end, we will see, Java Function Interface is the Java Functional interface.

1. What is Java functional Interface?

The interface which has only one abstract method is called Java Functional interface. Why they are so important because they are small simple and moreover everywhere in Java 8 Stream API. Java functional interfaces are annotated with @FunctionalInterface. Which enforces the programmer to keep only one method for that interface and throws the compilation error.

@FunctionalInterface
interface Country{
  String getState();
  default String getName(){
    return "CountryName";
  }
}

The above example a simple example of a functional interface. It surely has two methods but it has only one abstract method which doesn’t have any definition. For more functional interface you can read this blog.

2. Java Function Interface

Java Function Interface is the Java Functional interface. It has only one method and it’s signature restrict the programmer to define the method in that format only. The method accepts a parameter and returns another type of variable.

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
    ...
}

As per the method signature, you can see it takes a variable of type T and returns variable of type R. Below are some examples where it can be used.

//To find a square you had to create a class and a method. 
class Util{
  public static int square(int i){
    return i*i;
  }
}

Util.square(5); // return 25.

// Using Function interface.
Function<Integer,Integer> square = i -> i*i;
square.apply(5); // return 25.

//Another example
Function<String,Integer> strLength = s -> s.length(); // equivalent to String::length
strLength.apply("Sanjay") // returns 6

3. Chaining Function Interface

Function interface provides two methods two chain it to another Function interface. Depending on the method it either executes the first Function interface before or after the provided Function interface

3.1 Executing another Function Interface before current.

This method chains the  Function interface and executes the parameter Function interface before the caller Function.

 "1,"+s;
Function<String, String> appendTwo= s -> "2, "+s;

Function<String, String> finalString = appendOne.compose(appendTwo);
finalString.apply("3"); // print 1,2,3

3.2 Executing another Function Interface after current.

This method chains the  Function interface and executes the parameter Function interface before the caller Function.

 "1,"+s;
Function<String, String> appendTwo= s -> "2, "+s;

Function<String, String> finalString = appendOne.andThen(appendTwo);
finalString.apply("3"); // print 2,1,3,

4. Java Function Interface is the Java Functional interface

By now you must have figured out Function Interface is actually an interface which is annotated by @funcationInterface and has an only single method to do small function based logic.

5. Conclusion

In this blog, we have discussed the Java function interface and what is are functional interfaces. Also, how Function interface can be chained easily.