Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use lambda expressions in java8

Shulou Source: shulou.com Published: 2022-06-02 08:27:20 09月16日 Update

This article introduces how to use lambda expression in java8, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1.lambda indicates that it can be used everywhere. It is only used in functional interfaces.

2, for example, Chestnut has such an interface.

Public interface LumbdaTestinterface {String test (String name);}

What if I want an implementation object? The first method writes another class implementation interface and overrides the test method

The second method is to create an anonymous implementation class directly when you use it, such as this

LumbdaTestinterface lumbdaTestinterface= new LumbdaTestinterface () {@ Override public String test (String name) {return "slim" + name;}}

The lambda expression simplifies the above anonymous implementation as follows

LumbdaTestinterface testLambdaService = (name)-> {return "slim" + name;}

The parentheses in front of the parameters are written directly, the multiple parameters are separated by commas (the type can be omitted), and the method is written directly in parentheses after the arrow. There is only one parameter, and parentheses can be left unwritten. If you don't wrap the line after the arrow, you can save curly braces and return.

three. This implementation doesn't even have a method name, so why can you implement the test method? This is mentioned in 1, and this writing requires that the class can only have one abstract method, so this writing is the method to be implemented by default.

On how to use lambda expressions in java8 to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Methods parentheses parameters interfaces expressions slim content writing that is more arrows help good interest functions only name capital place object boy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn OPPO Reno Microsoft Shulou Information