Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining multiple IntStream [duplicate]

I am trying to get certain integers, all which I am getting over the stream, however, they are essentially being added to a new stream of integers which I will be using later on.

To populate the new stream of integers, I make multiple IntStreams and then append them to a new one using the IntStream builder, as such:

Is there a better way to approach this:

    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);
like image 242
slees 2043 Avatar asked Oct 20 '25 13:10

slees 2043


1 Answers

Whenever seeing multiple streams, I think about flatten. Hope it helps.

Stream.of(rightSide, leftSide, top, bottom).flatMapToInt(x -> x)
like image 99
Duong Nguyen Avatar answered Oct 22 '25 02:10

Duong Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!