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,内部实现会检查抛异常。
网友评论