[英]iterator remove still leaves the element in the set
I have this code: 我有以下代码:
for (Iterator<RuleConditionBl> iterator = ruleConditionBls.iterator(); iterator.hasNext();) {
RuleConditionBl ruleConditionBl = iterator.next();
if (ruleConditionBl.equals(ruleCondition)) {
iterator.remove();
}
}
if (ruleConditionBls.size() == 0)
{
countryToNonSplittedRules.remove(country);
}
I see ruleConditionBls.size == 1
before and after this line is executed iterator.remove();
我在执行此行之前和之后看到
ruleConditionBls.size == 1
iterator.remove();
how can i fix this? 我怎样才能解决这个问题?
following JB Nizet
answer, i have changed my code and it now works: 在
JB Nizet
回答之后,我更改了代码,现在可以使用:
It uses the current hashCode to find the bucket where the element is located, and since the hashCode has changed, it doesn't find it.
Set<RuleConditionBl> ruleConditionBls = countryToNonSplittedRules.get(country);
if (ruleConditionBls != null) {
final Set<RuleConditionBl> collect = ruleConditionBls.stream().filter(condition -> !condition.equals(ruleCondition)).collect(Collectors.toSet());
if (collect.size() == 0)
{
countryToNonSplittedRules.remove(country);
}
else
{
countryToNonSplittedRules.put(country, collect);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.