官术网_书友最值得收藏!

Lambda expressions

Lambda expressions are the brand new feature of Java. Lambda expressions are introduced in Java 8 and it is a step towards facilitating functional programming in Java.

Lambda expressions help you to define a method without declaring it. So, you do not need a name for the method, return-type, and so on. Lambda expressions, like anonymous inner classes, provide the way to pass behaviors to functions. Lambda, however, is a much more concise way of writing the code.

For example, the preceding example of an anonymous inner class can be converted to Lambda as follows:

public class MyFilterImpl { 
   public static void main(String[] args) { 
      File dir = new File("src/main/java"); 
      dir.list((dirname,name)->name.endsWith("java")); //Lambda Expression 
     } 
} 

Note that the signature of the Lambda expression is exactly matching the signature of the accept method in the FilenameFilter interface.

One of the huge differences between Lambda and anonymous inner classes lies in the way Lambdas are invoked. Lambdas are invoked dynamically using Method Handle ( MH) calls, unlike anonymous inner classes where a separate inner class is created at compile time for reference.
主站蜘蛛池模板: 五大连池市| 乌兰察布市| 疏勒县| 门头沟区| 巴彦县| 安义县| 乌兰察布市| 玛纳斯县| 泰顺县| 陕西省| 平顶山市| 西华县| 蒲城县| 行唐县| 鹿泉市| 浮山县| 太谷县| 宜阳县| 锡林郭勒盟| 阳高县| 金平| 周宁县| 阿克陶县| 南宁市| 七台河市| 丰县| 昭平县| 临汾市| 乡城县| 巴彦淖尔市| 台北县| 安国市| 纳雍县| 宁津县| 天峨县| 连山| 万安县| 启东市| 康马县| 当涂县| 龙胜|