Explicitly-typed lambda expressions

A lambda expression that explicitly specifies the type of all of its input parameters is referred to as an explicitly-typed lambda expression. The following code illustrates a few examples (with the input parameters in bold):

(Integer age) -> age > 10;                      // input Integer, 
// return Boolean (Integer age) -> age > 10? "Kid" : "Not a Kid"; // input Integer,
// return String (Integer age) -> {System.out.println();}; // input Integer,
// return void () -> {return Math.random() + "Number";}; // input none,
// return String (String name, List<Person> list) -> { return ( list.stream() .filter(e ->
e.getName().startsWith(name)) .map(Person::getAge) .findFirst() ); }; // input name,
// List<person>
// return
// Optional<Integer>

The code in all of the preceding examples explicitly defines the types of all of the parameters that are being passed to it. If a lambda expression doesn't accept any parameter, it uses a pair of empty round braces (()).

If this makes you wonder the types of the variables to which the lambda expressions will be assigned, here's the complete code for your reference:

Predicate<Integer> predicate = (Integer age) -> age > 10; 
Function<Integer, String> function = (Integer age) -> age > 10? "Kid" : 
"Not a Kid";
Consumer<Integer> consumer = (Integer age) -> {
System.out.println();
}; Supplier<String> supplier = () -> {
return Math.random() +
"Number";
}; BiFunction<String, List<Person>,
Optional<Integer>> firstElement = (String name, List<Person> list) -> { return ( list.stream() .filter(e ->
e.getName().
startsWith(name)) .map(Person::getAge) .findFirst() ); }; class Person { int age; String name; String getName() { return name; } Integer getAge() { return age; } }
..................Content has been hidden....................

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