Chapter: Java The Complete Reference - The Java Language - Lambda Expressions

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Generic Functional Interfaces

A lambda expression, itself, cannot specify type parameters. Thus, a lambda expression cannot be generic.

Generic Functional Interfaces

 

A lambda expression, itself, cannot specify type parameters. Thus, a lambda expression cannot be generic. (Of course, because of type inference, all lambda expressions exhibit some “generic-like” qualities.) However, the functional interface associated with a lambda expression can be generic. In this case, the target type of the lambda expression is determined, in part, by the type argument or arguments specified when a functional interface reference is declared.

To understand the value of generic functional interfaces, consider this. The two examples in the previous section used two different functional interfaces, one called NumericFunc and the other called StringFunc. However, both defined a method called func( ) that took one parameter and returned a result. In the first case, the type of the parameter and return type was int. In the second case, the parameter and return type was String. Thus, the only difference between the two methods was the type of data they required. Instead of having two functional interfaces whose methods differ only in their data types, it is possible to declare one generic interface that can be used to handle both circumstances. The following program shows this approach:

 

 

     //Use a generic functional interface with lambda expressions.

 

//A generic functional interface.

 

interface SomeFunc<T> { T func(T t);

 

}

 

class GenericFunctionalInterfaceDemo { public static void main(String args[])

 

{

 

// Use a String-based version of SomeFunc. SomeFunc<String> reverse = (str) -> {

 

String result = ""; int i;

 

for(i = str.length()-1; i >= 0; i--) result += str.charAt(i);

 

return result;

 

};

 

System.out.println("Lambda reversed is " + reverse.func("Lambda"));

 

System.out.println("Expression reversed is " + reverse.func("Expression"));

 

// Now, use an Integer-based version of SomeFunc.

SomeFunc<Integer> factorial = (n) -> {

 

int result = 1;

 

for(int i=1; i <= n; i++) result = i * result;

 

return result;

 

};

 

System.out.println("The factoral of 3 is " + factorial.func(3)); System.out.println("The factoral of 5 is " + factorial.func(5));

 

}

 

}

The output is shown here:

 

Lambda reversed is adbmaL

 

Expression reversed is noisserpxE

 

The factoral of 3 is 6

 

The factoral of 5 is 120

In the program, the generic functional interface SomeFunc is declared as shown here:

interface SomeFunc<T> { T func(T t);

 

}

 

Here, T specifies both the return type and the parameter type of func( ). This means that it is compatible with any lambda expression that takes one parameter and returns a value of the same type.

The SomeFunc interface is used to provide a reference to two different types of lambdas. The first uses type String. The second uses type Integer. Thus, the same functional interface can be used to refer to the reverse lambda and the factorial lambda. Only the type argument passed to SomeFunc differs.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.