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

Lambda expressions and functional interfaces

Lambda expressions are essential in functional programming. Lambda expressions are constructs that exist in a standalone fashion and not as a part of any class. One particular scenario where Lambda expressions can be used is while creating classes which consist of just a single method. Lambda expressions, in this case, help to be an alternative to anonymous classes (classes without names), which might not be feasible in certain situations. We will briefly look at two examples, side by side, of how we can convert a conventional Java snippet into a Lambda expression.

In the following code, we will assign a method to a variable called blockofCodeA. This is just what we are intending to solve with the means of Lambda expressions:

blockofCodeA = public void demo(){ System.out.println("Hello World");
}

The same piece of code can be written using Lambda expressions, as shown here:

blockofCodeA = () -> {                                 
System.out.println("Hello World");
}

Remove the name, return type, and the modifier, and simply add the arrow after the brackets. This becomes your Lambda expression.

主站蜘蛛池模板: 松江区| 姚安县| 仪征市| 大新县| 涟源市| 象山县| 高台县| 巴楚县| 苏尼特右旗| 福建省| 泊头市| 邵东县| 乾安县| 于田县| 平阴县| 泾源县| 杭州市| 思南县| 浑源县| 安溪县| 黎平县| 东丰县| 广宁县| 海宁市| 岱山县| 玉环县| 分宜县| 百色市| 会理县| 扎囊县| 会东县| 新郑市| 贵溪市| 松溪县| 普陀区| 同江市| 客服| 买车| 东山县| 历史| 民勤县|