search
HomeJavajavaTutorialWhat are the uses of lambda expressions, the new features of Java 8 (usage examples)

We have been waiting for a long time for lambda to bring the concept of closure to Java, but if we do not use it in collections, we will lose a lot of value. The problem of migrating existing interfaces to lambda style has been solved through default methods. In this article, we will deeply analyze the bulk data operation (bulk operation) in Java collections and unravel the mystery of the most powerful role of lambda.

1. About JSR335

JSR is the abbreviation of Java Specification Requests, which means Java specification request. The main improvement of Java 8 version is the Lambda project (JSR 335), whose purpose is to make Java more Easy to write code for multi-core processors. JSR 335=lambda expression + interface improvement (default method) + batch data operation. Together with the previous two articles, we have completely learned the relevant content of JSR335.

2. External VS internal iteration

In the past, Java collections were not able to express internal iteration, but only provided one way of external iteration, that is, a for or while loop.

List persons = asList(new Person("Joe"), new Person("Jim"), new Person("John"));
for (Person p :  persons) {
   p.setLastName("Doe");
}

The above example is our previous approach, which is the so-called external iteration. The loop is a fixed sequence loop. In today's multi-core era, if we want to parallelize the loop, we have to modify the above code. How much the efficiency can be improved is still uncertain, and it will bring certain risks (thread safety issues, etc.).

To describe internal iteration, we need to use a class library like Lambda. Let’s use lambda and Collection.forEach to rewrite the above loop

persons.forEach(p->p.setLastName("Doe"));

Now the jdk library controls the loop. We don't need to care about how the last name is set to 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 internal iteration, and the client passes the behavior p.setLastName as data into the api.

Internal iteration is actually not closely related to the batch operation of the collection. With its help, we can feel the changes in grammatical expression. The really interesting thing related to batch operations is the new stream API. The new java.util.stream package has been added to JDK 8.


3.Stream API

Stream only represents a data stream and has no data structure, so after it has been traversed once, it can no longer be accessed. Traversal (you need to pay attention to this when programming, unlike Collection, there is still data in it no matter how many times it is traversed), its source can be Collection, array, io, etc.

3.1 Intermediate and end-point methods

The function of the stream is to provide an interface for operating big data, making data operations easier and faster. It has methods such as filtering, mapping, and reducing the number of traversals. These methods are divided into two types: intermediate methods and terminal methods. The "stream" abstraction should be continuous by nature. Intermediate methods always return a Stream, so if we want to get the final result If so, endpoint operations must be used to collect the final results produced by the stream. The difference between these two methods is to look at its return value. If it is a Stream, it is an intermediate method, otherwise it is an end method. Please refer to Stream's API for details.

A brief introduction to several intermediate methods (filter, map) and end-point methods (collect, sum)

3.1.1Filter

The first step is to implement the filtering function in the data stream The most natural operation we can think of. The Stream interface exposes a filter method, which accepts a Predicate implementation representing an operation to use a lambda expression that defines filter conditions.

List persons = …
Stream personsOver18 = persons.stream().filter(p -> p.getAge() > 18);//过滤18岁以上的人

3.1.2Map

Suppose we filter some data now, such as when converting objects. The Map operation allows us to execute an implementation of Function (the generic T and R of Function represent execution input and execution result respectively), which accepts input parameters and returns them. First, let's see how to describe it as an anonymous inner class:

Stream adult= persons
              .stream()
              .filter(p -> p.getAge() > 18)
              .map(new Function() {
                  @Override
                  public Adult apply(Person person) {
                     return new Adult(person);//将大于18岁的人转为成年人
                  }
              });

Now, convert the above example into a lambda expression:

Stream map = persons.stream()
                    .filter(p -> p.getAge() > 18)
                    .map(person -> new Adult(person));

3.1.3Count

The count method is the end-point method of a stream, which can make the final statistics of the stream results and return int. For example, we calculate the total number of people who are 18 years old or older:

int countOfAdult=persons.stream()
                       .filter(p -> p.getAge() > 18)
                       .map(person -> new Adult(person))
                       .count();

3.1.4Collect

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

List adultList= persons.stream()
                       .filter(p -> p.getAge() > 18)
                       .map(person -> new Adult(person))
                       .collect(Collectors.toList());

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

List adultList = persons
                 .stream()
                 .filter(p -> p.getAge() > 18)
                 .map(person -> new Adult(person))
                 .collect(Collectors.toCollection(ArrayList::new));

The space is limited, other intermediate The methods and endpoint methods will not be introduced one by one. After reading the above examples, you only need to understand the difference between these two methods, and you can decide to use them according to your needs later.

3.2 Sequential Stream and Parallel Stream

Each Stream has two modes: sequential execution and parallel execution.
Sequential flow:

List <Person> people = list.getStream.collect(Collectors.toList());

Parallel flow:

List <Person> people = list.getStream.parallel().collect(Collectors.toList());

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

3.2.1 Parallel stream principle:

List originalList = someData;
split1 = originalList(0, mid);//将数据分小部分
split2 = originalList(mid,end);
new Runnable(split1.process());//小部分执行操作
new Runnable(split2.process());
List revisedList = split1 + split2;//将结果合并

3.2.2 Sequential and parallel performance test comparison

If it is a multi-core machine, theoretically parallel stream will be faster than sequential stream Double the above, the following is the test code

long t0 = System.nanoTime();
//初始化一个范围100万整数流,求能被2整除的数字,toArray()是终点方法
int a[]=IntStream.range(0, 1_000_000).filter(p -> p % 2==0).toArray();
long t1 = System.nanoTime();
//和上面功能一样,这里是用并行流来计算
int b[]=IntStream.range(0, 1_000_000).parallel().filter(p -> p % 2==0).toArray();
long t2 = System.nanoTime();
//我本机的结果是serial: 0.06s, parallel 0.02s,证明并行流确实比顺序流快
System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9);

3.3 About Folk/Join framework

Application hardware parallelism is available in Java 7, that is, one of the new features of the java.util.concurrent package is a fork-join style parallel decomposition framework, which is also very powerful and efficient. Interested students Go and research, I won’t go into details here. Compared to Stream.parallel(), I prefer the latter.

4. Summary

If there is no lambda, Stream is quite awkward to use. It will generate a large number of anonymous internal classes, such as the 3.1.2map example above. If there is no default method, the collection framework will change. It is bound to cause a lot of changes, so lambda+default method makes the jdk library more powerful and flexible. The improvements of Stream and collection framework are the best proof.

For more Java8 new features, what are the uses of lambda expressions (usage examples) and related articles, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.