Java 8:lambdas and the new collection stream API

Source: Internet
Author: User
Tags stream api

Lambda is a key feature of Java8, Java 8:lambdas & Java Collections | The Zeroturnaround.com article describes methods for processing large amounts of data using a lambda collection.

First, the Java collection introduced internal traversal, the original Lambdaj below this method can also be implemented in JAVA8:


list<person> persons = Aslist ( "Joe"new person ("Jim" New person ("John"));
ForEach (persons). Setlastname ("Doe");

Java8 is similar to this traversal in the following way:
Persons.foreach (P-p.setlastname ("Doe"))

Java8 JDK mainly introduces a new Api:stream API to handle Big Data, you can perform operations such as filter/map/reduce-with collections.

Use stream Apik to implement sequential or parallel operations on a data stream.



Sequential version
stream<person> stream = Persons.stream ();


stream<person> Parallelstream = Persons.parallelstream ();


Java.util.stream.Stream provides a way to manipulate a large number of data interfaces, and when a stream instance is obtained, the following interesting collection operations can be implemented:

1.Filter

Filtering is one of the first natural operations (like SQL where query statements)

list<person> persons = ...
stream<person> personsOver18 = Persons.stream (). Filter (P, P.getage () > 18);

(Banq Note: With such a powerful filtering function, after filtering the query do not always remember to use SQL statements, the load is concentrated on the database, if the amount of data larger than the hadoop/hive).

2. MAP
When we have filtered data, we are able to implement business operations such as object migration. If using an inner class is the following code:

stream<student> students = Persons.stream ()
. Filter (P, P.getage () > 18)
. Map (new Function<person, student> () {
@Override
Public Student apply (person person) {
return New Student (person);
}
});


But using lambda syntax is simple:

stream<student> map = Persons.stream ()
. Filter (P, P.getage () > 18)
New Student (person));

Because the Mao method parameter is a function parameter consumer, we can also pass in the following method:

stream<student> map = Persons.stream ()
. Filter (P, P.getage () > 18)
. Map (Adult::new);
We have already implemented the operation of the data flow, and finally we need to collect and summarize the results of these data, collect () Let us achieve this:


list<student> students = Persons.stream ()
. Filter (P, P.getage () > 18)
. Map (Adult::new)
. Collect (new collector<student, list<student>> () {...});

The above implements its own collector interface, which may not be required in most cases:


list<student> students = Persons.stream ()
. Filter (P, P.getage () > 18)
. Map (Student::new)
. Collect (Collectors.tolist ());


You can also specify a collection-specific type:

list<student> students = Persons.stream ()
. Filter (P, P.getage () > 18)
. Map (Student::new)
. Collect (Collectors.tocollection (ArrayList::new));



For order and parallel operations:
The Stream API does not require an explicit declaration order or parallelism, is parallel when consumer, and then switches to the order:


list<student> students = Persons.stream ()
. Parallel ()
. Filter (P, p.getage () >) //Filtering'll be performed concurrently
. Sequential ()
. Map (Student::new)
. Collect (Collectors.tocollection (ArrayList::new));

The filter function defaults to parallel operations.

Java 8:lambdas and the new collection stream API

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.