Home  >  Article  >  Java  >  Stream API in Java 8: How to use map() method to perform transformation operations on collections

Stream API in Java 8: How to use map() method to perform transformation operations on collections

WBOY
WBOYOriginal
2023-07-29 10:33:131633browse

The Stream API was introduced in Java 8, providing a new way to operate data collections. The stream API allows us to operate on collections in a simpler and more flexible way, in which the map() method is a commonly used conversion operation.

Before Java 8, when we convert a collection, we usually need to use a for loop or iterator to traverse the elements in the collection and then process them one by one. Using the map() method of the stream allows us to convert the collection more conveniently, simplifying the operation process.

First, let’s understand the definition of the map() method. The map() method is an intermediate operation in the stream API. It accepts a function as a parameter. This function will be applied to each element in the stream and map it into a new element. Finally, the map() method returns the stream containing the new elements.

The following is a sample code that uses the map() method to convert a collection:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class MapExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        List<Integer> squaredNumbers = numbers.stream()
                .map(n -> n * n)
                .collect(Collectors.toList());

        System.out.println("原始集合: " + numbers);
        System.out.println("转换后的集合: " + squaredNumbers);
    }
}

In the above sample code, we first create a collection numbers containing integers. Then, we call the stream() method to convert it to a stream. Next, we call the map() method and pass a function (n -> n * n) as a parameter. What this function does is map each element n in the stream to n squared. Finally, we use the collect() method to collect the converted elements into a new list.

Run the above code, the output is as follows:

原始集合: [1, 2, 3, 4, 5]
转换后的集合: [1, 4, 9, 16, 25]

You can see that each element in the original collection has been squared, and the new elements are collected into a new list .

In addition to the above examples, we can also use the map() method to implement more complex conversion operations. For example, you can convert all strings in a string collection to uppercase, or extract a certain attribute of an object in a collection to form a new collection, etc. The map() method can convert elements according to the function logic we pass, and is very flexible.

In summary, the stream API in Java 8 provides a convenient way to perform conversion operations on collections. The map() method is one of its core methods. It can map the elements in the stream to new elements according to the function we pass, and return the stream containing the new elements. By using the map() method, we can convert collections more concisely and flexibly, improving the readability and maintainability of the code.

The above is the detailed content of Stream API in Java 8: How to use map() method to perform transformation operations on collections. For more information, please follow other related articles on 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