A simple Java Function interface example: Learn Functional programming fast

If you want to master functional programming, the best place to start is with the Java Function interface. This example will show you four different ways to implement this functional interface in your code — starting with how to use an actual class, and how to create very concise code with a lambda function.

The Java Function interface is quite simple. It takes a single Java object as an argument, and returns a single Java object when the method concludes. Any method you can conjure up takes an object and returns an object that fulfills the Java Function contract.

How to use Java’s Function interface

For this Java Function interface example, we will provide a single method named “apply” that takes an Integer as an argument, squares it and returns the result as a String. Before we begin, let’s take a quick look at the official JavaDoc for java.util.function.Function:

Java Interface Function<T,R>

Parameter Types:
T - the input given to the function
R - the result running the function

Popular Subinterface of Function: UnaryOperator<T>

The JavaDoc also indicates that the Function interface has a single, non-default method named apply which takes a single argument and returns a single argument:

R apply(T t)

People often mistake that there is something magical about the interfaces defined in the java.util.functions package, but there’s not. They are just normal interfaces, and as such, we can always create a class that explicitly implements them.

class FunctionClass implements Function<Integer, String> {
  public String apply(Integer t) {
    return Integer.toString(t*t);

The FunctionClass defined here implements Function and provides an implementation for the apply method. We could then use this in any class with standard syntax rules.

Function<Integer, String> functionClass = new FunctionClass();

When you run the above code, the output would be four.

Java Function interface example

Similarly, we can write a Java Function example that uses an inner class to implement the apply method:

Function<Integer, String> innerClass = new Function<Integer, String>(){
  public String apply(Integer t) {
    return Integer.toString(t*t);

When you run the inner class example, the output would be nine.

Java’s Function and lambda expression example

Of course, the whole idea of the functional interface is to incorporate lambda expressions into the mix. Here’s the Java Function example implemented with a rather verbose lambda expression:

Function<Integer, String> verboseLambda = (Integer x)-> { return Integer.toString(x*x); };

This implementation will print out the value 25. Of course, this implementation is also very wordy. With a Java lambda expression, the object type isn’t required on the left hand side, and if the lambda expression is one line long, both the brackets and the return keyword can be omitted. So a more concise lambda expression that implements this Java Function interface would look like this:

Function<Integer, String> conciseLambda = (Integer x)-> { return Integer.toString(x*x); };

When the code implements Java Function with a concise lambda expression runs, the program prints out 25.

That’s all you really need to know about the java.util.function.Function interface. It is a very simple component that simply requires the implementation of one method — named apply — that is passed in a single object, runs to completion and returns another Java object. It’s just that simple.

Don’t overcomplicate Java’s Function interface

Some people can find the simplicity of the Java Function interface to be a bit confusing. After all, a method that takes something and returns something else seems to be so incredibly vague and abstract that is almost seems meaningless.

However, seasoned Java developers know that sometimes the simplest of language constructs can turn out to be the most powerful, as is the case with object models designed with abstract classes and interfaces. Power through simplicity is exactly the point when it comes to the various components listed in the java.util.function package.

Java Function Example

The Java Function interface is used in many parts of the java.util.function API package

You will run into the Function interface in a variety of places, especially when you start advanced functional programming with the Java Streams API. Powerful methods such as map, reduce and flatMap all take a Java Function as a parameter. So if you plan on any map-reduce programming with Java, Functions will become one of your biggest friends.

App Architecture
Software Quality
Cloud Computing