Why doesn't ImmutableMap.builder().build() pick the correct type parameters?

后端 未结 1 863
离开以前
离开以前 2021-01-17 14:46

Why does Map test = ImmutableMap.builder().build(); fail to compile, but Map test = ImmutableMap.b

1条回答
  •  醉梦人生
    2021-01-17 15:39

    This is not a failure in Guava, but rather in the way Java resolves generics, and it's something we can't control. =(

    Believe us: this is something that we spent a lot of time on. In this issue, Kevin mentions that we attempted no less than fifteen approaches for trying to get it so that you didn't have to explicitly specify these type parameters.

    If you're only interested in the case of ImmutableMap.builder().build(), that is, with no entries in the map...then you should be using ImmutableMap.of(). That won't have any funny issues with generics: it'll just work.

    0 讨论(0)
提交回复
热议问题