我试过这段代码:
final ImmutableMap<String, String> map1 = ImmutableMap.of("username", userName, "email", email1, "A", "A1",
"l", "500L");
final ImmutableMap<String, String> map2 = ImmutableMap.of("b", "ture", "hashed_passwords", "12345", "e",
"TWO", "fakeProp", "fakeVal");
final ImmutableMap<String, String> map3 = ImmutableMap.builder().putAll(map1).putAll(map2).build();
但收到了错误:
Error:(109, 105) java: incompatible types: com.google.common.collect.ImmutableMap<java.lang.Object,java.lang.Object> cannot be converted to com.google.common.collect.ImmutableMap<java.lang.String,java.lang.String>
我怎么能把它投射呢?
答案 0 :(得分:0)
Java类型推断并不总是像我们所有人希望的那样好。有时,您需要提供类型提示以使其能够解决问题。
在您的情况下,您需要为ImmutableMap构建器方法提供显式的泛型类型。
ImmutableMap.<String, String>builder()
.putAll(map1)
.putAll(map2)
.build();