When does Java require explicit type parameters?

前端 未结 3 1272
眼角桃花
眼角桃花 2020-12-15 08:38

Given:

import com.google.common.collect.ImmutableMap;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
         


        
3条回答
  •  -上瘾入骨i
    2020-12-15 09:14

    There's another way to fix this. You can give a hint to compiler explicitly specifying the argument type for identity lambda:

    ImmutableMap map2 = Stream.of("1", "2", "3")
        .collect(Testcase.toImmutableMap((String i) -> i, Integer::valueOf).build());
    

    Compiles fine in Javac 1.8.0_25 and ECJ 3.10.2.

提交回复
热议问题