I was trying following code snippet to understand the collect
method on IntStream
to understand. I was trying to result Hashmap <string, Integer>
.
IntStream.range(0, 4).collect(
HashMap::new,
result, t -> result.put("test", somearray.get(t)),
result -> result.putall()
);
But compilation complains, can't find symbol variable result
.
As per my understanding I need to pass (t, value) -> ...
to accumulatore
, but I am not able to understand compilation issue as well the use of combiner (3rd argument).
You are missing some brackets there... Besides look at the definitions of IntStream.collect
it takes two parameters : ObjIntConsumer
and BiConsumer
. Both take two arguments as input and return nothing.
int somearray[] = new int[] { 1, 5, 6, 7 };
HashMap<String, Integer> map = IntStream.range(0, 4).collect(
HashMap::new,
(result, t) -> result.put("test" + t, somearray[t]),
(left, right) -> left.putAll(right));
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