What are the benefits of using lambda expressions in java 8?

Interface AccountService{ public void createAccount(); } AccountService accountServiceAnonymous = new AccountService(){ public void createAccount(){ Account account = new Account(); save(account); } }; AccountService accountServiceLambda = () -> { Account account = new Account(); save(account); } 

Besides the reduced number of lines of code, are there any other advantages to using lambda expressions in java 8?

+7
java lambda java-8
source share
5 answers

Adding to what @Bilbo mentioned in the comments. Java 1.7 introduced a new Opcode JVM called invokedynamic , and Java 8 Lambda uses this. Thus, the following code will create an anonymous class when compiling the code. Maybe <ClassName>$1.class , so if you have 10 anonymous classes that have 10 more classes in the final bank.

 AccountService accountServiceAnonymous = new AccountService(){ public void createAccount(){ Account account = new Account(); save(account); } }; 

But Java 8 lambda uses invokedynamic to call lambdas, so if you have 10 lambdas, this will not lead to any anonymous classes, thus reducing the size of the last jar.

 AccountService accountServiceLambda = () -> { Account account = new Account(); save(account); } 
+12
source share

Another advantage of lambdas (and method references) is seen when combining them with the Stream API and other functions added in Java 8, for example. Not necessary.

Consider this code:

 private void pushToFront(AbstractInfo contactInfo) { registeredWindows.stream() .filter(window -> window.getWindowId() == contactInfo.getId()) .findAny() .ifPresent(Window::pushToFront); } 

The method filters the list of registered windows corresponding to the window, with the contact identifier returning optional. If the list contains a window with a matching identifier, then the value Optional is present, and then the pushToFront method will be pressed on it. Compare this with the same functionality written in Java 7:

 private void pushToFront(AbstractInfo contactInfo) { for (Window window : registeredWindows) { if (window.getWindowId() == contactInfo.getId() { window.pushToFront(); } } } 

Code with a lambda expression, a thread reference, and a method, at least for me, is more concise and readable (when you use streaming). The above example is pretty simple, but consider one that in Java 7 requires nested loops, multiple conditional statements, etc. It is not easy to read even more difficult, so as not to lose information about what is happening.

Lambdas makes full use of other neat Java 8 features that, among other things, provide neat, clean, efficient, and clear code.

On the bottom line, you should consider lambda expressions as part of a larger whole that are big for yourself, but even better when combined with other Java 8 building blocks.

+2
source share

One more - unlike anonymous classes, lambdas DO NOT create a new scope, they have the same scope as the block / environment spanning.

So:

  • It's easier to access the closing object - a simple this reference refers to an instance of the incoming class (and you don't need to say EnclosingClass.this )

  • No shadowing issues (since you cannot define local variables with the same names as the variables in the scope)

+1
source share

In addition to what was said here, and in order to strictly answer the question posed, it is important to see lambdas as a β€œcode block”, which, among other things, can be passed as a parameter. This gives a huge advantage due to the fact that you can remove duplicate code .

Like this? In the example provided by @Kitke, the pushToFront (...) method, if this requirement exists, can be reconstructed a bit as a template and used to filter registered Windows using any condition . The expression window ambda -> window.getWindowId () == contactInfo.getId () can be passed as a parameter in this case. Without this powerful feature, you need to write a while loop every time you need to filter a registered Windows collection in a different state. Huge win, think about your code with this.

Extracted from: http://www.copypasteisforword.com/notes/lambda-expressions-in-java . There you can find another example of using lambda to remove duplicate code.

0
source share

The advantage of lambda expressions 1. It reduces the line of code. 2. It supports sequential and parallel execution, passing behavior in methods with the collection thread API. 3. Using the Stream API and lambda expression, we can achieve higher efficiency (parallel execution) in the case of mass operations on collections.

0
source share

All Articles