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

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.
主站蜘蛛池模板: 介休市| 维西| 宜兰县| 石屏县| 句容市| 阳新县| 宁远县| 咸宁市| 喜德县| 福安市| 涿州市| 白银市| 慈利县| 义马市| 新乡市| 静海县| 四川省| 延吉市| 太湖县| 马边| 年辖:市辖区| 台中市| 建湖县| 含山县| 平湖市| 中牟县| 花垣县| 张家口市| 都昌县| 海南省| 洞口县| 年辖:市辖区| 关岭| 诸暨市| 和林格尔县| 兰考县| 巴彦县| 拉萨市| 通渭县| 新龙县| 汉源县|