java 8 stream boxed() 怎么用
时间: 2024-04-29 09:22:45 浏览: 21
Java 8中的Stream API提供了一个boxed()方法,可以将基本类型的流转换为对应的包装类型流。boxed()方法可以用于IntStream、LongStream和DoubleStream。
例如,如果有一个IntStream,可以使用boxed()方法将其转换为一个Stream<Integer>,如下所示:
```
IntStream intStream = IntStream.of(1, 2, 3);
Stream<Integer> stream = intStream.boxed();
```
同样的方法可以用于LongStream和DoubleStream:
```
LongStream longStream = LongStream.of(1L, 2L, 3L);
Stream<Long> stream = longStream.boxed();
DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0);
Stream<Double> stream = doubleStream.boxed();
```
在使用boxed()方法之后,可以对流进行常规的Stream操作,例如map、filter、reduce等。
相关问题
java 8 stream boxed()
The `boxed()` method is a terminal operation in Java 8 stream that returns a `Stream` consisting of the elements of the original stream, boxed to their corresponding wrapper classes.
For example, if you have a `Stream<Integer>` and you want to convert it to a `Stream<Object>`, you can use the `boxed()` method as follows:
```
Stream<Integer> intStream = Stream.of(1, 2, 3, 4, 5);
Stream<Object> objStream = intStream.boxed();
```
The `boxed()` method is useful when you need to perform operations on a stream of primitive values, such as `IntStream`, `LongStream`, or `DoubleStream`, but your code requires a `Stream` of objects.
For example, if you have an `IntStream` and you want to find the average of its elements, you can use the `average()` method, which returns an `OptionalDouble`. However, if you need to return the average as a `Double` instead of an `OptionalDouble`, you can use the `boxed()` method to convert the `IntStream` to a `Stream<Integer>` and then use the `mapToDouble()` method to convert it to a `DoubleStream`:
```
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
double avg = intStream.average()
.orElse(0.0);
```
This code returns the average as an `OptionalDouble`. To return it as a `Double`, you can use the `boxed()` method as follows:
```
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
double avg = intStream.boxed()
.mapToDouble(Integer::doubleValue)
.average()
.orElse(0.0);
```
java stream拆分数组
Java Stream可以使用Arrays类的静态方法stream()来拆分数组。例如,如果我们有一个整数数组,可以使用以下代码将其拆分为流:
int[] array = {1, 2, 3, 4, 5};
Stream<int[]> stream = Arrays.stream(array).boxed();
在这个例子中,我们首先使用Arrays.stream()方法将整数数组转换为IntStream,然后使用boxed()方法将IntStream转换为Stream<Integer>,这样我们就得到了一个包含整数的流。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)