简体   繁体   English

Java-8:要流式传输的布尔原始数组?

[英]Java-8: boolean primitive array to stream?

There is no nice way to convert given boolean[] foo array into stream in Java-8 in one statement , or I am missing something?没有好的方法可以在一个语句中将给定的boolean[] foo数组转换为 Java-8中的流,或者我遗漏了什么?

(I will not ask why? , but it is really incomprehensible: why not add stream support for all primitive types?) (我不会问 为什么? ,但确实令人费解:为什么不为所有原始类型添​​加流支持?)

Hint: Arrays.stream(foo) will not work, there is no such method for boolean[] type.提示: Arrays.stream(foo)不起作用, boolean[]类型没有这样的方法。

Given boolean[] foo use给定boolean[] foo使用

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

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 ).请注意,每个布尔值都将被装箱,但这通常不是大问题,因为布尔值装箱不会分配额外的内存(仅使用预定义值之一 - Boolean.TRUEBoolean.FALSE )。

You can use Guava's Booleans class :您可以使用Guava 的Booleans

Stream<Boolean> stream = Booleans.asList(foo).stream();

This is a pretty efficient way because Booleans.asList returns a wrapper for the array and does not make any copies.这是一种非常有效的方法,因为Booleans.asList返回数组的包装器并且不进行任何复制。

of course you could create a stream directly当然你可以直接创建一个流

Stream.Builder<Boolean> builder = Stream.builder();
for (int i = 0; i < foo.length; i++)
  builder.add(foo[i]);
Stream<Boolean> stream = builder.build();

…or by wrapping an AbstractList around foo ...或者通过foo周围包裹一个AbstractList

Stream<Boolean> stream = new AbstractList<Boolean>() {
  public Boolean get(int index) {return (foo[index]);}
  public int size() {return foo.length;}
}.stream();

Skimming through the early access JavaDoc (ie. java.base module) of the newest , there is still no neat way to make the primitive boolean array work with Stream API together well.浏览最新的的早期访问 JavaDoc(即java.base模块),仍然没有巧妙的方法使原始布尔数组与 Stream API 一起工作。 There is no new feature in the API with treating a primitive boolean array since .以来,API 中没有处理原始布尔数组的新功能

Note that there exist IntStream , DoubleStream and LongStream , but nothing like BooleanStream that would represent of a variation of a sequence of primitive booleans.请注意,存在IntStreamDoubleStreamLongStream ,但没有像BooleanStream那样表示原始布尔值序列的变体。 Also the overloaded methods of Stream are Stream::mapToInt , Stream::mapToDouble and Stream::mapToLong , but not Stream::mapToBoolean returning such hypothetical BooleanStream . Stream的重载方法还有Stream::mapToIntStream::mapToDoubleStream::mapToLong ,但不是Stream::mapToBoolean返回这种假设的BooleanStream

Oracle seems to keep following this pattern, which could be found also in Collectors . Oracle 似乎一直遵循这种模式,这也可以在Collectors找到。 There is also no such support for float primitives (there is for double primitives instead).也没有对float基元的这种支持(而是对double基元的支持)。 In my opinion, unlike of float , the boolean support would make sense to implement.在我看来,与float不同的是,实现boolean支持是有意义的。

Back to the code... if you have a boxed boolean array (ie. Boolean[] array ), the things get easier:回到代码......如果你有一个装箱的布尔数组(即Boolean[] array ),事情会变得更容易:

Boolean[] array = ...
Stream<Boolean> streamOfBoxedBoolean1 = Arrays.stream(array);
Stream<Boolean> streamOfBoxedBoolean2 = Stream.of(array);

Otherwise you have to use more than one statement as said in this or this answer.否则,您必须在说使用一个以上的声明这个答案。

However, you asked ( emphasizes mine ):但是,你问(强调我的):

way to convert given boolean[] foo array into stream in Java-8 in one statement.一个语句中将给定的boolean[] foo 数组转换为 Java-8 中的流的方法。

... there is actually a way to achieve this through one statement using a Spliterator made from an Iterator . ...实际上有一种方法可以通过使用由Iterator制成的Spliterator语句来实现这一点。 It is definetly not nice but :这绝对不是很好,但是:

boolean[] array = ...

Stream<Boolean> stream = StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(
                new Iterator<>() {
                    int index = 0;
                    @Override public boolean hasNext() { return index < array.length; }
                    @Override public Boolean next() { return array[index++]; }
                }, 0), false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM