Java Generic Functional Interface

https://‮tual.www‬turi.com
Java Generic Functional Interface

In Java, a functional interface is an interface that has a single abstract method. A functional interface can be used as the target type of a lambda expression or method reference.

A generic functional interface is a functional interface that has a single abstract method with generic type parameters. A generic functional interface can be used to represent a function that takes one or more arguments of a specific type and returns a value of a specific type.

Here's an example of a generic functional interface in Java:

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

This interface defines a function that takes an argument of type T and returns a value of type R. The @FunctionalInterface annotation indicates that this is a functional interface.

You can use the Function interface as the target type of a lambda expression or method reference as follows:

Function<Integer, String> intToString = i -> i.toString();
System.out.println(intToString.apply(123)); // prints "123"

Function<String, Integer> stringToInt = Integer::parseInt;
System.out.println(stringToInt.apply("456")); // prints "456"

In this example, the intToString variable is assigned a lambda expression that converts an integer to a string, and the stringToInt variable is assigned a method reference that converts a string to an integer. The apply() method of the Function interface is used to apply the function to the given argument.

Created Time:2017-11-03 22:21:03  Author:lautturi