Introduction to Java 8 Lambda expressions

Source: Internet
Author: User

Introduction to Java 8 Lambda expressions

Lambda expressions are an important feature of Java 8. After being released for so long, I recently learned about the concept of closures in swift, which is actually very similar to Lambda expressions, so today I will sort out the Lambda expressions in Java 8 and give an introduction.

Lambda expressions actually provide support for functional programming in Java programming. When we get used to everything as an object, it may be hard to read Lambda just getting started, but after getting used to it, it does help the code to be concise, and has similar concepts like Lambda in Python and swift. It may be used in the future, so it is helpful to study it.

Lambda basic syntax: ([parameter…]) -> {Statements ;}// [] The table is optional, that is, the brackets can have no parameters.

Next we will learn about Lambda through some examples:

I. Traversal

Java 8 adds the method forEach (Consumer action) to the Iterable iterator. The parameter is a Lambda expression, that is, a method. Next, let's look at the usage:

As shown in the figure, three lines of code are required in the past, which can be solved by one line, or even more concise, as shown below:

Here: The symbol is a scope, and then a specific method, for example, Objest: hashCode. In the same way, System. out is actually a PrintStream object, and the PrintStream object has a Println method.

In addition, Arrays is used here. the asList () method may often be used to return the List. However, note that Arrays. asList () returns java. util. an internal class of ArraysJava. util. Arrays. ArrayList <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Coding + coding/coding + DQo8YnIgLz4NCjxpbWcgYWx0PQ = "" src = "http://www.bkjia.com/uploads/allimg/160408/04303aS0-2.png" title = "\"/>

Ii. Runnable

Lambda expressions can greatly simplify the code of anonymous internal classes, as follows:

You can also simplify the process as follows:

Iii. Arrays sorting

Arrays. sort () is also frequently used. Let's look at the sorting of lambda expressions:

As shown in the figure, lambda expressions can complete the required functions in one line, streamline a lot of code, and get used to high readability in the future. Here we will write this example to show you howLambda has multiple parametersAs forArrays. stream ()I will write an article later to talk about it and itsMap, reduce, filterMethod. You only need to know that the returned Stream object is a Stream object. You can pass in a lambda expression to traverse and filter the object.

4. Predicate

When translating Predicate into assertions, it indicates that a lambda expression that returns true and false needs to be input here. Let's first look at its usage:

The return value of the test method of Predicate is bool. When looking at its source code, we find that there are methods such as and or. The following describes the possible application scenarios of Predicate and filters an array:

We can see that the filter method has passed in two lambda expressions to determine whether the string length is greater than 4 and whether it ends with A. In the filter method, it is connected through and, you can also change to or to try the result. The returned List contains only the required data.

V. Others

Note that because lambda expressions are equivalent to anonymous internal classes, the referenced external parameters must beFinaSo it cannot be assigned a value. Like the anonymous internal class, an error will be reported during the compilation, as shown below:

If you have any questions about the new stream of Java 8, you can leave a message after paying attention to the public account.

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.