So I am having trouble understanding the principle of this supplier ,accumulator and combiner in the collect from IntStream. I found an example.
IntStream.range(6,11)
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append);
Anyone mind explaining the StringBuilder::appendCodePoint part?
Let's wrap this stream to make byte[] out of it and then String from the resulting array:
Arrays.toString(IntStream.range(6,11).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString().getBytes())
That would give us:
[6, 7, 8, 9, 10]
It takes the stream of 6..10 and collects it in the following order:
StringBuilder instanceStringBuilder::appendCodePoint to an accumulator. (equivalent to: (StringBuilder sb, int x) -> sb.appendCodePoint(x))StringBuilder instance created in (1). (equivalent to: (StringBuilder sb1, StringBuilder sb2) -> sb1.append(sb2))The type signature of this parametrized collect() call looks like this:
collect(Supplier<StrinBuilder> supplier,
ObjIntConsumer<StrinBuilder> accumulator,
BiConsumer<StrinBuilder, StrinBuilder> combiner);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With