Java Reference
In-Depth Information
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 determ-
ined, in part, by the type argument or arguments specified when a functional interface ref-
erence is declared.
To understand the value of generic functional interfaces, consider this. Earlier in this
chapter, two different functional interfaces were created, one called NumericTest and the
other called StringTest . They were used to determine if two values satisfied some con-
dition. To do this, both defined a method called test( ) that took two parameters and re-
turned a boolean result. In the case of NumericTest , the values being tested were integers.
For StringTest , the values were of type String . Thus, the only difference between the two
methods was the type of data they operated on. Such a situation is perfect for generics. In-
stead 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:
Search WWH ::




Custom Search