Lexical Scoping in Closures

 
public​ ​static​ ​Predicate​<​String​> checkIfStartsWith(​final​ ​String​ letter) {
 
return​ name -> name.startsWith(letter);
 
}

From within a lambda expression we can access variables that are in the enclosing method’s scope. For example, the variable letter in the checkIfStartsWith is accessed within the lambda expression. Lambda expressions that bind to variables in enclosing scopes are called closures.

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.