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;
            }
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM