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

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.

主站蜘蛛池模板: 镇雄县| 太谷县| 铅山县| 临潭县| 灵台县| 乌拉特前旗| 蒙阴县| 永嘉县| 侯马市| 蓝山县| 洛南县| 全椒县| 大丰市| 汤阴县| 安多县| 霸州市| 老河口市| 卢龙县| 额济纳旗| 中江县| 原阳县| 大厂| 泰州市| 安新县| 宾川县| 永定县| 西和县| 西安市| 即墨市| 永嘉县| 太和县| 合作市| 饶平县| 三穗县| 宜黄县| 清远市| 筠连县| 金山区| 涞水县| 顺义区| 繁峙县|