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

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.

主站蜘蛛池模板: 垫江县| 古蔺县| 平顶山市| 海安县| 聊城市| 栾川县| 景德镇市| 交口县| 兴国县| 门源| 淄博市| 长泰县| 墨玉县| 监利县| 濮阳县| 太湖县| 东光县| 楚雄市| 白城市| 海晏县| 内黄县| 信阳市| 太原市| 会昌县| 镇原县| 修水县| 三穗县| 东兰县| 台东县| 庄河市| 金山区| 桐柏县| 五华县| 洪雅县| 乌兰县| 仁布县| 贵阳市| 湘西| 清苑县| 新邵县| 武鸣县|