Java-8:boolean原始数组要流吗? [英] Java-8: boolean primitive array to stream?

查看:153
本文介绍了Java-8:boolean原始数组要流吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有很好的方法将给定的 boolean [] foo 数组转换为Java-8 中的一个语句,或者我遗失了什么?

There is no nice way to convert given boolean[] foo array into stream in Java-8 in one statement, or am I missing something?

(我不会问为什么?,但实在是难以理解:为什么不为所有原始类型添​​加流支持?)

(I will not ask why?, but it is really incomprehensible: why not add stream support for all primitive types?)

提示: Arrays.stream(foo)将没有用, boolean [] 类型没有这样的方法。

Hint: Arrays.stream(foo) will not work, there is no such method for boolean[] type.

推荐答案

给定 boolean [] foo 使用

Stream<Boolean> stream = IntStream.range(0, foo.length)
                                  .mapToObj(idx -> foo[idx]);

请注意,每个布尔值都会被装箱,但它通常不是一个大问题,因为装箱布尔值不分配额外的内存(只使用预定义的值之一 - Boolean.TRUE Boolean.FALSE )。

Note that every boolean value will be boxed, but it's usually not a big problem as boxing for boolean does not allocate additional memory (just uses one of predefined values - Boolean.TRUE or Boolean.FALSE).

这篇关于Java-8:boolean原始数组要流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆