Java 8 new features (ii)

Source: Internet
Author: User

Batch processing of collection classes:

Java8 provides another important feature in addition to the lambda expression, which is the batch operation of the collection, The purpose of the batch operations API for the collection class is to implement the "internal iteration" of the collection class and expect to take advantage of modern multicore CPUs for parallel computing. The iteration (iteration) of the collection class before Java8 is external, that is, the customer code. An internal iteration means that the Java class Library is iterated instead of the customer code.

For example Java8 the previous version to traverse a collection is usually:

for (Object o:list) {System.out.println (o);}

In Java8, you can work with lambda expressions:

List.foreach (O-System.out.println (o));

There is a Foreach method in the collection class, and the parameter of the method is a function interface consumer, which has an accept () method to accept a single parameter, and in the implementation of the Foreach method, the Accept () parameter is the element that is obtained each time the collection is traversed. The Foreach method uses only lambda to make the code more concise relative to the previous for loop, but it is also a serial operation of each element, and there is no elevation relative to the for loop performance, whereas the previously mentioned parallel execution is due to the stream introduced in the collection.

The stream definition given in the Java documentation is:

A sequence of elements supporting sequential and parallel aggregate operations.

This means that stream is a sequence of elements that supports sequential and concurrent collection processing operations.

stream interface under the Java.util.stream package, The main implementations are Intstream, Longstream, Doublestream, etc., the stream object is created mainly in two ways:

    1. By using the default method of the collection interface, stream () creates

    2. Created by the static factory method of the Stream interface

The stream interface provides methods that are broadly divided into two categories:

    1. A class of elements of the original stream filtering, transformation, processing, and so on, and finally return a processed new stream object, can be chained calls in a number of ways to work together

    2. A class is after the new stream is processed to obtain the desired non-stream type result, such as the collection, the maximum value in the stream element, the number of elements remaining after filtering, etc.

Stream Part I found a very detailed blog to write, want to further study can refer to http://ifeve.com/stream/


Use of double colons:

Java8: This operator, which is used to pass a reference to a method or construct method, is primarily used to simplify a lambda expression, such as an example of the above foreach method, which can be simplified to:

List.foreach (System.out::p rintln)

There are some limitations to using this method to simplify lambda, first of all the code in the lambda expression code block must be called directly to another method, and the number, order, and type of the method must be the same as the abstract method in the corresponding interface. You also need to get a reference to the constructor method, just string::new this, after the class:: New to


Java 8 new features (ii)

Related Article

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.