美文网首页
Multiple entries with same key

Multiple entries with same key

作者: 鹅鹅鹅_ | 来源:发表于2019-08-09 17:20 被阅读0次

Java异常模拟代码如下:

public static void main(String[] args) {
    new HashMap<String, String>(){{
        put("1", "1");
        put("1", "1");
    }};
    ImmutableMap.<String, String>builder()
            .put("1", "1")
            .put("1", "1")
            .build();
}

异常信息如下:

Exception in thread "main" java.lang.IllegalArgumentException: Multiple entries with same key: 1=1 and 1=1
    at com.google.common.collect.ImmutableMap.conflictException(ImmutableMap.java:214)
    at com.google.common.collect.ImmutableMap.checkNoConflict(ImmutableMap.java:208)
    at com.google.common.collect.RegularImmutableMap.checkNoConflictInKeyBucket(RegularImmutableMap.java:146)
    at com.google.common.collect.RegularImmutableMap.fromEntryArray(RegularImmutableMap.java:109)
    at com.google.common.collect.ImmutableMap$Builder.build(ImmutableMap.java:392)
    at com.gome.tccb.domain.job.ModelPlanningWorkflowStarter.main(ModelPlanningWorkflowStarter.java:415)

原因是ImmutableMap是不可变Map,如果已经设置了一个特定的key1,则不能再次设置覆盖key1,内部实现会检查抛异常。

相关文章

网友评论

      本文标题:Multiple entries with same key

      本文链接:https://www.haomeiwen.com/subject/twnhjctx.html