树型数据遍历之ConcurrentModificationException

场景

最近需要去遍历修改树型数据中的叶子节点,数据结构如下:

"data": [
    {
        "id": 102528, 
        "name": "一级代理", 
        "children": [
            {
                "id": 480896, 
                "name": "二级代理", 
                "parentId": 102528, 
                "children": [
                    {
                        "id": 571520, 
                        "name": "门店", 
                        "parentId": 480896
                    }
                ]
            }
        ]
    }
]

于是乎写了一个简单的递归函数

private static List<Map<String, Objtect>> treeNode(Long id, List<Map<String, Object>> list) throws Exception {
	for (Map<String, Object> item : list) {
		if (id.equals(item.get("id"))) {
			list.remove(item);
		}
		if (item.containsKey("children") && null != item.get("children")) {
			List<Map<String, Object>> childrens = (List<Map<String, Object>>) item.get("children");
			treeNode(id, childrens);
		}
	}
	return list;
}

结果报错java.util.ConcurrentModificationException报错信息中给出的错误位置在checkForComodification(), 查看此方法源码:

final void checkForComodification() {
	if (modCount != expectedModCount)
		throw new ConcurrentModificationException();
}
代码修改

上面的代码在调用list.remove(item)后会导致modCount != expectedModCount,我们改为用iterator.remove()可解决此问题。

private static List<Map<String, Object>> treeNode(Long id, List<Map<String, Object>> list) throws Exception {
	Iterator iterator = list.iterator();
	while (iterator.hasNext()) {
		Map<String, Object> item = (Map<String, Object>) iterator.next();
		if (id.equals(item.get("id"))) {
			iterator.remove();
		}
		if (item.containsKey("children") && null != item.get("children")) {
			List<Map<String, Object>> childrens = (List<Map<String, Object>>) item.get("children");
			treeNode(id, childrens);
		}
	}
	return list;
}

至于原因,大家看下ArrayList的内部类Itr源码,会发现在remove()方法里重新赋值了expectedModCount = modCount

private class Itr implements Iterator<E> {
	public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值