加入多个IntStream

sle*_*043 3 java java-stream

我试图得到某些整数,我将通过流来获取所有整数,但是,它们基本上被添加到我将在稍后使用的新整数流中.

为了填充新的整数流,我创建了多个IntStream,然后使用IntStream构建器将它们附加到新的IntStream,如下所示:

有没有更好的方法来解决这个问题:

    IntStream rightSide = IntStream.range(8, this.rows.getSlotAmount()).map(value -> value + 9);
    IntStream leftSide = IntStream.range(0, this.rows.getSlotAmount()).map(value -> value % 9);
    IntStream top = IntStream.range(0, 9);
    IntStream bottom = IntStream.range(this.rows.getSlotAmount() - 9, this.rows.getSlotAmount());

    IntStream.Builder slots = IntStream.builder();

    rightSide.forEach(slots::add);
    leftSide.forEach(slots::add);
    top.forEach(slots::add);
    bottom.forEach(slots::add);

    slots.build().forEach(this::methodReference);
Run Code Online (Sandbox Code Playgroud)

Duo*_*yen 6

每当看到多个流时,我都会想到扁平化.希望能帮助到你.

Stream.of(rightSide, leftSide, top, bottom).flatMapToInt(x -> x)
Run Code Online (Sandbox Code Playgroud)