我是靠谱客的博主 舒服嚓茶,这篇文章主要介绍map的循环删除操作,现在分享给大家,希望可以做个参考。

1.错误示例

Map<String,InterfaceOutParam> outCodes1 = outParamList.stream().collect(Collectors.toMap(InterfaceOutParam::getOutparamCode,a->a));
Map<String,InterfaceOutParam> outCodes2 = outParamList2.stream().collect(Collectors.toMap(InterfaceOutParam::getOutparamCode,a->a));
//统计修改的参数
for (String key : outCodes1.keySet()) {
if(outCodes2.containsKey(key)){
updateOutParamList.add(outCodes1.get(key));
outCodes1.remove(key);
continue;
}
}

抛出异常:

2.使用 Iterator 遍历删除

for (Iterator<Map.Entry<String, InterfaceOutParam>> it = outCodes1.entrySet().iterator(); it.hasNext();){
Map.Entry<String, InterfaceOutParam> item = it.next();
if(outCodes2.containsKey(item.getKey())){
updateOutParamList.add(outCodes1.get(item.getKey()));
it.remove();
continue;
}
}

 

转载于:https://www.cnblogs.com/UncleWang001/p/10531466.html

最后

以上就是舒服嚓茶最近收集整理的关于map的循环删除操作的全部内容,更多相关map内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部