Java 8 – Generic Functional Interfaces

Learn to create generic functional interfaces with and without type restrictions in Java 8 and later. Note that functional interfaces permit exactly one abstract method. These interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

1. Generic functional interface without type restriction

1.1. Interface definition

A functional interface can be defined that is generic for type X and has a functional method that accepts two arguments of type X and returns a value of type X.

package com.howtodoinjava.java8.example;

@FunctionalInterface
public interface ArgumentsProcessor<X> 
{
    X process(X arg1, X arg2);
}

This interface can be used for any type i.e. ArgumentsProcessor<Integer>, ArgumentsProcessor<String> or ArgumentsProcessor<Employee>.

1.2. Example

Java example to use generic functional interface with type Integer.

package com.howtodoinjava.java8.example;

public class Main 
{
	public static void main(String[] args)
    {
		ArgumentsProcessor<Integer> multiplyProcessor = new ArgumentsProcessor<Integer>() {
            @Override
            public Integer process(Integer arg1, Integer arg2)
            {
                return arg1 * arg2;
            }
        };
        
        System.out.println(multiplyProcessor.process(2,3));  	//6
    }
}

Java example to use generic functional interface with type String.

package com.howtodoinjava.java8.example;

public class Main 
{
	public static void main(String[] args)
    {
		ArgumentsProcessor<String> appendProcessor = new ArgumentsProcessor<String>() {
            @Override
            public String process(String str1, String str2)
            {
                return str1  + " " + str2;
            }
        };
        
        System.out.println(appendProcessor.process("Hello", "World !!"));	//Hello World !!
    }
}

2. Generic functional interface with type restriction

2.1. Interface definition

A functional interface can be defined that is restricted to certain types using extends keyword i.e. X extends Number.

package com.howtodoinjava.java8.example;

@FunctionalInterface
public interface ArgumentsProcessor<X extends Number> 
{
    X process(X arg1, X arg2);
}

This interface can be used for any type i.e. ArgumentsProcessor<Integer>, ArgumentsProcessor<Double> but not for ArgumentsProcessor<String> or ArgumentsProcessor<Employee>.

In above example, the permitted type must extend the Number class.

2.2. Example

Java example to use generic functional interface with type Integer.

package com.howtodoinjava.java8.example;

public class Main 
{
	public static void main(String[] args)
    {
		ArgumentsProcessor<Double> doubleMultiplier = new ArgumentsProcessor<Double>() {
            @Override
            public Double process(Double arg1, Double arg2)
            {
                return arg1 * arg2;
            }
        };
        
        System.out.println(doubleMultiplier.process(4d, 6d));	//24.0
    }
}

3. Specialized Functional Interface

Specialization is accomplished by extending or implementing the generic functional interface of one type. The resulting interface or class is not generic for that type.

package com.howtodoinjava.java8.example;

@FunctionalInterface
public interface ArgumentsProcessor<Integer> 
{
    Integer process(Integer arg1, Integer arg2);
}
package com.howtodoinjava.java8.example;

public class Main {
	public static void main(String[] args)
    {
		ArgumentsProcessor<Integer> intMultiplier = (i1, i2) -> i1 * i2;

        System.out.println(intMultiplier.process(4, 5));	//20
    }
}

Drop me your questions related to functional interfaces with generics.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.