---
title: "hashmap的remove异常问题"
author: "Thomas会Coding"
date: "2015-03-30T16:39:51Z"
tags: []
categories: ["编程语言"]
slug: "hashmap-remove"
draft: false
lastmod: "2015-03-30T16:39:51Z"
id: 2197130
url: "https://zzcjobstudy.iteye.com/blog/2197130"
---

jdk为什么这样设计，只允许通过iterator进行remove操作？  
HashMap和keySet的remove方法都可以通过传递key参数删除任意的元素，而iterator只能删除当前元素(current)，一旦删除的元素是iterator对象中next所正在引用的，如果没有通过modCount、 expectedModCount的比较实现快速失败抛出异常，下次循环该元素将成为current指向，此时iterator就遍历了一个已移除的过期数据。