New features JDK8 of --Lambda kinky odd expression skills

Foreword

JDK8 has released almost 4 years, and now talk about its new features seem a little bit "outdated." Although JDK8 is no longer "new", but it's one of the important characteristics --Lambda expression is still not being proficient most developers use, it is not even known to the developer.

Domestic development environment we all know, there are all kinds of old projects, publishing a wide variety of risks, so that the company and the project team for the new technology is often prohibitive, and even today there are companies still using JDK6 to project development this has led many in the choice of technology to be greatly restricted, and thus can not follow the pace of the times make the project even company step by step decline.

This paper briefly recognize --Lambda expression of one of the important new features JDK8. Before JDK8, Java does not support functional programming, so-called functional programming, to understand is a function (also known as the "act") as an argument. We have to mention generally more object-oriented programming, object-oriented programming is an abstract data (various POJO class), and the programming function is abstract behavior (the behavior will be passed as a parameter) . In JavaScript this is a very common characteristic of grammar, but in Java will be a function passed as an argument that it does not work, fortunately JDK8 appeared to break the limitations of Java.

Lambda expressions awareness

First, let's introduce an example, I do not know whether to have experience writing code in IDEA, if prepared under JDK8 Java environment is shown below in accordance with the traditional rules of grammar a thread.

IDEA will prompt Lambda expressions can be used interchangeably .

Lambda expressions only need to use a word you can use instead of using anonymous class way above.

In this example, the traditional rules of grammar, we are an anonymous inner class as a parameter is passed, we realized the Runnable interface, and pass it as an argument to the Thread class, which in fact we passed a piece of code, that is, we place the code as the data transfer, which brought a lot of unnecessary "boilerplate code."

Lambda expressions are a total of three parts:

Later examples we will explain the structure, including the availability of parameters, with or without the return of value. So this looks weird syntax rules does not look like Java, which itself in the end what is the meaning of it? It also started to bother my question, when and under what scenarios you can use Lambda expressions.

** Lambda expressions can receive parameter types is a method of containing only one interface. ** interface contains a single method called " function interface ."

Such as creating a thread of the example above, only the Runnable interface contains a method, it is called "function interface", it may be used in place of expressions Lambad anonymous inner classes. According to this rule, we try to write a function interface, and use Lambda expressions passed as a parameter.

test:

It can be seen that, as long as only one interface comprises a method, you can use Lambda expressions, such an interface called "function interface."

The above function of the interface is relatively simple does not contain parameters, nor the return value .

Let's modify FunctionInterface function interface to gradually increase the difficulty of Lambda expressions - contains parameters, do not include a return value .

test:

Watch Lambda expressions "(x) -> Sysout.out.println ( " Hello World "+ x)", is the parameter passed to the left, does not specify where the parameter type, because it can be deduced by the type of context, but in in some cases can not deduce the parameter type (typically at compile time can not be derived prompts IDE), then the need to indicate the parameter type. I personally recommend, specify the type of the function parameter under any circumstances .

In any case you can not derive parameter type? It is a generic function interface is the time.

test:

The above-mentioned two exemplary cases Lambda expressions:

No parameters and returns no value;

There are parameters, no return value.

The next step is there are parameters, return values case this more complicated.

test:

At this time, Lambda expressions "(Integer x) -> true", the right is the expression of the subject, the direct return true if more than one line of code, you can use curly braces directly, for example:

Lambda expressions basic grammar rules:

No parameters and returns no value, () -> System.out.println ( "Hello World");

Has parameters and returns no value, (x) -> System.out.println ( "Hello World" + x);

It has parameters, return values, (x, y) -> x + y.

The three basic situation has been generally clear, in particular the need to clarify, when you can use Lambda expressions instead of anonymous inner classes, that is, application scenarios Lambda expressions are a function of the interface. Lambda expressions introducing this new feature in JDK8, the greater benefit is a collection of API updates, new Stream library that we use when traversing a collection is no longer as continue to use a for loop.

JDK8 using the correct posture collections

Example: calculate the number of students from "chengdu" how much.

Before JDK8 code:

JDK8 using the correct posture collection:

Using the API's "difficulty" is like a raise, and really just not familiar with it. Traditional iterative approach requires Read the full cycle in order to understand the code logic, JDK8 way you can flow through the words too literally, and the code is greatly reduced.

The most important thing is --Stream flow. Stream tool is a complicated operation on the collection achieved by the functional programming mode. To explain in detail the implementation Stream I believe is not excessive to write a blog, so here are no longer examine the internal Stream implementation. Here to tell you, if you have the opportunity to use JDK8 development environment to develop, try to learn a new set operations API.

to sum up

Lambda expressions and above for functional programming just to an "understanding" of the state, seems to feel the reduced amount of code, we do not know the formula for Lambda more in-depth knowledge is more to do for the back or as a bedding a literacy paste, apply much about Lambda expressions, concurrent programming, reactive programming and so on. If you have about Lambda expressions or functional programming may wish to have a better insight to leave a comment.

Android development information architecture + interview information click on the link to receive free share

"Android Architect necessary to receive a free learning resources (architecture + video + interview thematic document study notes)"

Guess you like

Origin blog.csdn.net/Coo123_/article/details/93195236