OTHER-06-LAMBDA Study notes-LAMBDA

Source: Internet
Author: User

Lambda through the previous article we can basically see that lambda is a behavior very similar to the anonymous inner class of something, and in fact, most of the time it is to replace the anonymous inner class to achieve a more concise syntax.


You can see that this is a simple example of using LAMBDA instead of Comparator anonymous classes for sorting.    First of all, the code becomes more compact, the original need to use four lines and a half of the code is now half done, but this is not the most important, it is important that this from four to half of the line and half to make our code easier to chain operations.



You can see that if you still use the anonymous class, then this code is very ugly, but the use of LAMBDA is not the same, it is very natural support chain operation, and this code is very natural to read, first it is a function, it is multiplied by 2,andthen and then multiplied by 2, Andthen then multiply by 2. Readability is greatly improved compared to the previous anonymous class.

Predefined functional interfaces and chained LAMBDA through the above example, we also basically see that in some specific cases using lambda will greatly improve the readability of our code, note that under specific conditions, so it is a matter of reasonable judgment when and where to use lambda is a science.
And here we still want to introduce some of the tools we have, Java8 the LAMBDA, it is natural to provide us with a number of functional interfaces, with the support of these functional interfaces, our code can become more concise.



There are a lot of functional interfaces under the Java.util.function package, and here are some of the more common ones. Specifically, each function interface can be determined directly by looking at the parameters and the return value.
In the Java native system, a lot of interfaces like Runnable/comparator and so on are functional interfaces, and they all add a lot of interesting default methods, which can be said to produce a very interesting Lambda through these functional interfaces.

The method reference lambda itself is a Java abstraction of a function, it can be said that a lambda is a function (extension, the function that produces a lambda, it is a function of generating functions, called higher-order functions), and in Java classes in fact there are countless methods, they    Should be able to be a Lambda, and they do not implement a functional interface, so the concept of method reference is proposed. For any one method, it can be referenced by a specific rule, and it will be used as a LAMBDA expression after the reference.



Basically there's a way to quote support, and most of our code can become more elegant, and Java is a step closer to functional programming.

Practice a bit above, let's practice.



The first practice is a Supplier application, in the actual work often encounter such a thing, a model nested inside a model, and then a layer of nesting, when I actually just want the innermost layer of a certain property, must have a layer of peel off this object, But every peel of a layer will be carefully triggered nullpointerexception, this is really too much effort, and once the layer is very deep that is more trouble, so if you use such a Lambda to deal with this problem, it seems just good.
You can see that the LAMBDA is actually executed internally, and then try to catch the exception, if it is really empty, then return null, and return the true value if no exception is triggered.



The second practice is a sort of transformation, in the past if we were to use anonymous inner class to write this sort based on two or more attributes, then the solution is to write a nested if-else in a compare function, when the sorting properties are relatively small, it is barely acceptable, But if there are several attributes, the code is more laborious to write. But what if you try a Lambda?    In fact, this Lambda really is very simple, as long as you read him out, everything can be understood: first it is a compatator, it compares the ID of both, and then compare the age of both, easy to understand. Here we generally use a forced transformation to qualify the lambda type, because once the chained operation is added, it is somewhat difficult to infer the type of the lambda through the compiler, so write the type directly here.


From for notes (Wiz)

OTHER-06-LAMBDA Study notes-LAMBDA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.