重复键(尝试合并值 x 和 x)
Duplicate key (attempted merging values x and x)
我有这个简单的class
public class Rule {
int id;
long cableType;
我想将带有此 classes 的列表转换为 Map<Integer, Long>
我写了这段代码:
Map<Integer, Long> map = ruleList.stream().collect(Collectors.toMap(Rule::getId, Rule::getCableType));
但是在这个列表中我有像 (1, 10), (1,40)
这样的重复项
当我 运行 这个代码时,我得到这个异常:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 21 (attempted merging values 31 and 30)
我该如何解决这个问题?
为避免此错误,您需要以重复条目之一为例,为此您需要:
.collect(Collectors.toMap(Rule::getId, Rule::getCableType, (r1, r2) -> r1));
我有这个简单的class
public class Rule {
int id;
long cableType;
我想将带有此 classes 的列表转换为 Map<Integer, Long>
我写了这段代码:
Map<Integer, Long> map = ruleList.stream().collect(Collectors.toMap(Rule::getId, Rule::getCableType));
但是在这个列表中我有像 (1, 10), (1,40)
这样的重复项
当我 运行 这个代码时,我得到这个异常:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 21 (attempted merging values 31 and 30)
我该如何解决这个问题?
为避免此错误,您需要以重复条目之一为例,为此您需要:
.collect(Collectors.toMap(Rule::getId, Rule::getCableType, (r1, r2) -> r1));