java迭代删除一个文件夹,在迭代时从java中的集合中删除项目

I want to be able to remove multiple elements from a set while I am iterating over it. Initially I hoped that iterators were smart enough for the naive solution below to work.

Set set = new HashSet();

fillSet(set);

Iterator it = set.iterator();

while (it.hasNext()) {

set.removeAll(setOfElementsToRemove(it.next()));

}

But this throws a ConcurrentModificationException.

Note that iterator.remove() will not work as far as I can see because I need to remove multiple things at a time. Also assume that it is not possible to identify which elements to remove "on the fly", but it is possible to write the method setOfElementsToRemove(). In my specific case it would take up a lot of memory and processing time to determine what to remove while iterating. Making copies is also not possible because of memory constraints.

setOfElementsToRemove() will generate some set of SomeClass instances that I want to remove, and fillSet(set) will fill the set with entries.

After searching Stack Overflow I could not find a good solution to this problem but a few hours break later I realized the following would do the job.

Set set = new HashSet();

Set outputSet = new HashSet();

fillSet(set);

while (!set.isEmpty()) {

Iterator it = set.iterator();

SomeClass instance = it.next();

outputSet.add(instance);

set.removeAll(setOfElementsToRemoveIncludingThePassedValue(instance));

}

setOfElementsToRemoveIncludingThePassedValue() will generate a set of elements to remove that includes the value passed to it. We need to remove the passed value so set will empty.

My question is whether anyone has a better way of doing this or whether there are collection operations that support these kind of removals.

Also, I thought I would post my solution because there seems to be a need and I wanted to contribute the the excellent resource that is Stack Overflow.

解决方案

Normally when you remove an element from a collection while looping over the collection, you'll get a Concurrent Modification Exception. This is partially why the Iterator interface has a remove() method. Using an iterator is the only safe way to modify a collection of elements while traversing them.

The code would go something like this:

Set set = new HashSet();

fillSet(set);

Iterator setIterator = set.iterator();

while (setIterator.hasNext()) {

SomeClass currentElement = setIterator.next();

if (setOfElementsToRemove(currentElement).size() > 0) {

setIterator.remove();

}

}

This way you'll safely remove all elements that generate a removal set from your setOfElementsToRemove().

EDIT

Based on a comment to another answer, this may be more what you want:

Set set = new HashSet();

Set removalSet = new HashSet();

fillSet(set);

for (SomeClass currentElement : set) {

removalSet.addAll(setOfElementsToRemove(currentElement);

}

set.removeAll(removalSet);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值