无法理解在IntStream上使用collect()

sub*_*ngh 4 java java-8

我正在尝试使用代码片段来理解要理解的collect方法IntStream.我试图结果Hashmap <string, Integer>.

    IntStream.range(0, 4).collect(
            HashMap::new,
            result, t -> result.put("test", somearray.get(t)),
             result -> result.putall()
            );
Run Code Online (Sandbox Code Playgroud)

但汇编抱怨,can't find symbol variable result.

按我的理解,我需要传递(t, value) -> ...accumulatore,但我无法理解编译问题,以及使用组合(第三个参数)的.

Eug*_*ene 8

你在那里缺少一些括号......除了看IntStream.collect它的定义需要两个参数:ObjIntConsumerBiConsumer.两者都将两个参数作为输入而不返回任何内容

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));
Run Code Online (Sandbox Code Playgroud)