The role of lambda, a new feature of Java 8_ Power node Java college sorting

We've been looking forward to lambda bringing the concept of closure to Java for a long time, but we lose a lot of value if we don't use it in collections. The problem of existing interface migration becoming lambda style has been solved through default methods. In this article, we will deeply analyze the bulk operation in Java collection and unlock the mystery of the strongest function of lambda.

1. About jsr335

JSR is the abbreviation of Java specification requests, which means Java specification requests. The main improvement of Java 8 version is the lambda project (JSR 335), which aims to make it easier for Java to write code for multi-core processors.

2. External vs internal iteration

Previously, Java collections could not express internal iterations, but only provided a way of external iterations, that is, for or while loops.

The above example is our previous practice, that is, the so-called external iteration. The loop is a fixed sequential loop. In the current multi-core era, if we want to parallel loop, we have to modify the above code. How much efficiency can be improved is uncertain, and it will bring certain risks (thread safety issues, etc.).

To describe the internal iteration, we need to use a class library such as lambda. Next, we use lambda and collection Foreach overrides the above loop

Now the JDK library controls the loop. We don't need to care about how the last name is set in each person object. The library can decide how to do it according to the running environment, parallel, out of order or lazy loading. This is the internal iteration. The client passes the behavior p.setlastname into the API as data. In fact, internal iteration is not closely related to the batch operation of sets. With it, we can feel the changes in syntax expression. What's really interesting about batch operations is the new stream API. The new Java. Util. Stream package has been added to JDK 8.

3.Stream API

A stream only represents a data stream and has no data structure, so it can no longer be traversed after traversing it once (this should be noted in programming. Unlike collection, there is data in it after traversing it many times). Its sources can be collection, array, IO, etc.

3.1 intermediate and end point methods

The function of flow is to provide an interface to operate big data, making data operation easier and faster. It has methods such as filtering, mapping and reducing the number of passes. These methods are divided into two types: intermediate method and terminal method. The "flow" abstraction is inherently continuous. The intermediate method always returns a stream. Therefore, if we want to obtain the final result, we must use the terminal operation to collect the final result generated by the flow. The difference between the two methods depends on the return value. If it is stream, it is the intermediate method, otherwise it is the end method.

The following intermediate methods (filter, map) and endpoint methods (collect, sum) are briefly introduced

3.1. 1Filter

It is the most natural operation that we can think of first to implement the filtering function in the data flow. The stream interface exposes a filter method that can accept a predicate implementation representing an operation to use a lambda expression that defines a filter condition.

3.1. 2Map

Suppose we filter some data now, such as when converting objects. The map operation allows us to execute the implementation of a function (the generics T and R of function < T, R > represent execution input and execution result respectively), which accepts input parameters and returns. First, let's see how to describe it in the form of anonymous inner classes:

Now, convert the above example to a lambda expression:

3.1. 3Count

The count method is a flow end-point method, which can make the final statistics of the flow results and return int. for example, let's calculate the total number of people who meet the age of 18

3.1. 4Collect

The collect method is also an end-point method of the flow, which can collect the final results

Or, if we want to use a specific implementation class to collect results:

Due to limited space, other intermediate methods and end-point methods will not be introduced one by one. After looking at the above examples, we can understand the difference between the two methods. Later, we can decide to use them according to the needs.

3.2 sequential flow and parallel flow

Each stream has two modes: sequential execution and parallel execution.

Sequential flow:

Parallel stream:

As the name suggests, when traversing in a sequential manner, each item is read before the next item is read. When traversing in parallel, the array will be divided into multiple segments, each of which will be processed in different threads, and then the results will be output together.

3.2. 1. Principle of parallel flow:

If you know a little about Hadoop, you will know that MapReduce itself is a software framework for parallel processing of large data sets. Its core idea of processing big data is to be large and small, allocate it to different machines to run maps, and finally combine the results of all machines through reduce to get a final result, which is different from MapReduce, Stream uses multi-core technology to process big data through multi-core parallel processing, while MapReduce can be distributed.

3.2. 2 Comparison of sequential and parallel performance tests

If it is a multi-core machine, theoretically, the parallel flow will be twice as fast as the sequential flow. Here is the test code

3.3 about folk / join framework

The parallelism of application hardware exists in Java 7, which is Java util. One of the new features of concurrent package is a fork join style parallel decomposition framework, which is also very powerful and efficient. Interested students will study it. I won't discuss it in detail here. Compared with stream In parallel (), I prefer the latter.

4. Summary

Without lambda, stream is quite awkward to use. It will generate a large number of anonymous inner classes, such as 3.1.1 above 2map example, if there is no default method, the change of the collection framework is bound to cause a lot of changes. Therefore, lambda + default method makes the JDK library more powerful and flexible. The improvement of stream and collection framework is the best proof.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>