Java 8 Functional Interface Tutorial

In this blog post I would like to share with you how to use Java 8 Functional Interfaces and how to create your own functional interface.

What is Functional Interface?

Functional interface is a Java interface with single abstract method. Below is a very simple example of a functional interface:

package com.appsdeveloperblog;
 
@FunctionalInterface
public interface  MyFunction<A, R> {
     R myFunction(A a);
}

Please note the use of optional @FunctionalInterface annotation. This annotation is optional but the use of it helps you clearly communicate the purpose of this interface to other developers and to prevent some of your team members from adding more abstract methods to this interface. If @FunctionalInterface annotation is provided the code will not even compile if more than one abstract method is added to an interface and your functional interface is guaranteed to have only one single method and can be safely used as lambda expression.

Please also note that the functional interface above is parametrized with A  and where:
A – is a type of a method argument and
R – is a type of a return value.

So I could use this functional interface with a lambda expression in the following way:

             //A    //R               //argument  //returnValue
MyFunction<String, String> myFunction = (a) -> a;
System.out.println(myFunction.myFunction("Sergey"));

myFunction() will take a single method argument which is defined by A in my functional interface and it will return a value of type as defined in my functional interface above and the system.out.println() will print it out.

And this is it. At first it might look and sound very confusing but the more you use functional interfaces the better you become with them.

Let’s have a look as some functional interfaces that Java 8 has ready for us to use.

Java 8 Functional Interfaces

Have a look at the list of a ready to use functional interfaces defined in java.util.function package in Java 8. Let’s have a couple examples from this list.

ToIntFunction<T> – Represents a function that produces an int-valued result.

In the below code example, I will use the ToIntFunction<T> functional interface to convert a String value of 5 into an int value:

ToIntFunction<String> toIntFunction = (s) -> Integer.parseInt(s);
System.out.println(toIntFunction.applyAsInt("5"));

let’s take another example from this list:

DoubleFunction<R> – Represents a function that accepts a double-valued argument and produces a result.

and here is how to use the DoubleFunction<R> functional interface to convert a double value into a String value:

DoubleFunction<String> doubleFunction = (d) -> String.valueOf(d);
System.out.println(doubleFunction.apply(0.5));

Functional Interface with Two Arguments

You might have noticed that the above three examples I have shared with you accept only one method argument and produce a result. But it is also possible to have a functional interface that accepts two method arguments.

Have a look at the same list of functional interfaces in java.util.function package in Java 8. There are many functions that accept two method arguments and produce a result. For example:

ToIntBiFunction<T,U> – Represents a function that accepts two arguments and produces an int-valued result.

The ToIntBiFunction functional interface accepts two method arguments and should return an int value. Below is an example of how to use it.

ToIntBiFunction<String, String> myToIntBiFunction = (a,b)-> Integer.parseInt(a) + Integer.parseInt(b);
System.out.println(myToIntBiFunction.applyAsInt("1", "2"));

Custom Bi Functional Interface

We can also create our own custom Bi functional interface which will accept two method arguments of type String and will return a result of String type.

  1. Create a new Java interface with a name you like. For example: MyOwnBiFunction. Like so:
@FunctionalInterface
public interface MyOwnBIFunction<F, S, R> {
     R myFunction(F f, S s);
}

2.  Now, when the interface is created you can use it in your code:

// My own BI Function 
// MyOwnBIFunction<First argument, Second argument, Return type> 
MyOwnBIFunction<String, String, String> myBiFunction = (a, b) -> a + " " + b;
System.out.println(myBiFunction.myFunction("Sergey", "Kargopolov"));

I hope this short Java programming tutorial was helpful to you. I myself these days prefer learning by watching video courses and seeing code demonstrations. If you would like to learn more about Java, check out the below video courses and I hope you will find one of them useful to you.

Java Video Courses


Leave a Reply

Your email address will not be published. Required fields are marked *