Java-8: boolean primitive array to stream?

前端 未结 4 1824
梦毁少年i
梦毁少年i 2020-12-17 08:51

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

(I

4条回答
  •  独厮守ぢ
    2020-12-17 09:00

    Given boolean[] foo use

    Stream 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).

提交回复
热议问题