stream().map()
时间: 2023-10-12 18:13:58 浏览: 37
The `stream().map()` method is used in Java to transform the elements of a stream by applying a function to each element.
The `stream()` method is used to create a stream from a collection or an array.
The `map()` method takes a `Function` as an argument and applies it to each element of the stream, producing a new stream with the transformed elements.
For example, if we have a list of integers and we want to double each of them, we can use the `stream().map()` method as follows:
```
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> doubledNumbers = numbers.stream()
.map(n -> n * 2)
.collect(Collectors.toList());
System.out.println(doubledNumbers); // output: [2, 4, 6, 8, 10]
```
In this example, we first create a list of integers using `Arrays.asList()`. We then create a stream from this list using the `stream()` method. We use the `map()` method to apply the function `n -> n * 2` to each element of the stream, which doubles each number. Finally, we collect the transformed elements into a new list using the `collect()` method.