hashmap遍历中删除键值对

public class test {
    public static void main(String[] args) {
        Map<String, String> maps = new HashMap<String, String>();
        maps.put("10", "AA");
        maps.put("11", "BB");
        maps.put("12", "CC");
        maps.put("13", "DD");


        for (Iterator<Map.Entry<String, String>> it = maps.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, String> item = it.next();//此句不能少,否则,删除当前节点,指针无法找到下一节点
            String abc = Integer.toHexString(item.hashCode());
            it.remove();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/daijiguo/article/details/81008890
今日推荐