Implicitly-typed lambda expressions

A lambda expression that doesn't specify the types of any of its input parameters is referred to as an implicitly-typed lambda expression. In this case, the compiler infers the type of the method parameters and adds it to the bytecode.

Let's modify the lambda expression from the preceding section, dropping the types of the input parameters (the modified code is in bold):

(age) -> age > 10; 
(age) -> age > 10? "Kid" : "Not a Kid"; 
age -> {System.out.println();}; 
() -> {return Math.random() + "Number";}; 
 
(name, list) -> { 
                    return ( 
                        list.stream() 
                            .filter(e -> e.getName().startsWith(name)) 
                            .map(Person::getAge) 
                            .findFirst() 
                    ); 
                }; 

You can't mix implicitly-typed and explicitly-typed parameters in lambda expressions. For instance, the following code won't compile because it explicitly specifies type of x, but not for y:

(Integer x, y) -> x + y;                 // won't compile
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.15.161