参见英文答案 > Adding two Java 8 streams, or an extra element to a stream 8个
我试图得到某些整数,我将通过流来获取所有整数,但是,它们基本上被添加到我将在稍后使用的新整数流中.
为了填充新的整数流,我创建了多个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);
解决方法:
每当看到多个流时,我都会想到扁平化.希望能帮助到你.
Stream.of(rightSide, leftSide, top, bottom).flatMapToInt(x -> x)
标签:java,java-stream
来源: https://codeday.me/bug/20190828/1751145.html