Java,集合上的多个迭代器,删除正确的子集和ConcurrentModificationException [英] Java, multiple iterators on a set, removing proper subsets and ConcurrentModificationException

查看:200
本文介绍了Java,集合上的多个迭代器,删除正确的子集和ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套A = {(1,2),(1,2,3),(2,3,4),(3,4),(1)}

I have a set A = {(1,2), (1,2,3), (2,3,4), (3,4), (1)}

我想把它变成A = {(1,2,3),(2,3,4)},从这个集合中删除正确的子集。

I want to turn it into A={(1,2,3), (2,3,4)}, remove proper subsets from this set.

我正在使用HashSet来实现set,2迭代器来运行set并使用containsAll(c)检查所有对的正确子集条件,并使用remove()方法来删除正确的子集。

I'm using a HashSet to implement the set, 2 iterator to run through the set and check all pairs for proper subset condition using containsAll(c), and the remove() method to remove proper subsets.

代码如下所示:

HashSet<Integer> hs....
Set<Integer> c=hs.values();
Iterator<Integer> it= c.iterator();
while(it.hasNext())
{
    p=it.next();
    Iterator<Integer> it2= c.iterator();
    while(it2.hasNext())
    {
        q=it2.next();
        if q is a subset of p
            it2.remove();
        else if p is a subset of q
        {
            it.remove();
            break;
        }
    }
}

我得到一个ConcurrentModificationException 1时间我从内在的while循环中出来并做一个

I get a ConcurrentModificationException the 1st time i come out of the inner while loop and do a

p=it.next();

例外情况是在迭代时修改Collection。但这就是.remove()的用途。

The exception is for when modifying the Collection while iterating over it. But that's what .remove() is for.

我在使用1个迭代器时使用了remove()并且没有遇到任何问题。

I have used remove() when using just 1 iterator and encountered no problems there.

如果异常是因为我在迭代它时从'c'或'hs'中删除了一个元素,那么当它遇到下一个 2时应该抛出异常 .next()命令,但我没有看到它。我在遇到it.next()命令时看到它。

If the exception is because I'm removing an element from 'c' or 'hs' while iterating over it, then the exception should be thrown when it encounter the very next it 2 .next() command, but I don't see it then. I see it when it encounters the it.next() command.

我使用了调试器,并且在删除元素后,集合和迭代器处于完美的顺序。它们包含并指向正确的更新集和元素。 it.next()包含要分析的下一个元素,它不是一个已删除的元素。

I used the debugger, and the collections and iterators are in perfect order after the element has been removed. They contain and point to the proper updated set and element. it.next() contains the next element to be analyzed, it's not a deleted element.

关于我如何做我正在尝试做的事情而不做任何想法在我提交更新之前将hashset本身的副本用作中间体?

Any ideas over how i can do what i'm trying to do without making a copy of the hashset itself and using it as an intermediate before I commit updates?

谢谢

推荐答案

您无法使用 it2 修改集合,并继续使用进行迭代 。正如异常所说的那样,它是并发修改,并且它不受支持。

You can't modify the collection with it2 and continue iterating it with it. Just as the exception says, it's concurrent modification, and it's not supported.

我担心你会遇到中间集合。

I'm afraid you're stuck with an intermediate collection.

实际上,你的代码似乎没有意义:你确定它是整数的集合而不是设置<整数> ?在您的代码 p q 整数 s,所以如果q是p的一个子集似乎没有多大意义。

Actually, your code doesn't seem you make sense: are you sure it's a collection of Integer and not of Set<Integer>? In your code p and q are Integers, so "if q is a subset of p" doesn't seem to make too much sense.

一种显而易见的方法可以让它变得更聪明:先按大小排序,当您从最大到最小时,将要保留的那些添加到新列表中。您只需要根据 keep 列表检查每个集合,而不是整个原始集合。

One obvious way to make this a little smarter: sort your sets by size first, as you go from largest to smallest, add the ones you want to keep to a new list. You only have to check each set against the keep list, not the whole original collection.

这篇关于Java,集合上的多个迭代器,删除正确的子集和ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆