Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call remove on a Set iterator #189

Closed
voodooless opened this issue Jun 14, 2012 · 2 comments

Comments

Projects
None yet
2 participants
@voodooless
Copy link

commented Jun 14, 2012

When I try to call remove on a set iterator, I get a classcastexception:

Iterator<Long> setIterator = someSet.iterator();
while (setIterator.hasNext()) {
    if(somecondition(setIterator)){
        setIterator.remove();
    }
}

This gets the following exception:

java.lang.ClassCastException: com.hazelcast.impl.SetProxyImpl cannot be cast to com.hazelcast.impl.IRemoveAwareProxy
    at com.hazelcast.impl.Entries$EntryIterator.remove(Entries.java:166) ~[hazelcast-all-2.1.2.jar:2.1.2]

Is this by design, or is this a bug? In either case: how can i work arround this?

@ghost ghost assigned mdogan Jun 14, 2012

@mdogan

This comment has been minimized.

Copy link
Member

commented Jun 14, 2012

Bug. You can call set.remove(key). (Since Hazelcast gives caller a copy-iterator, this works without a concurrency issue.)

Iterator<Long> setIterator = someSet.iterator();
while (setIterator.hasNext()) {
    if(somecondition(setIterator)) {
        someSet(setIterator.next());
    }
}

For example;

Collection<Integer> set = Hazelcast.getSet("set");
for (int i = 0; i < 100; i++) {
    set.add(i);
}
System.out.println("Size-Before: " + set.size());
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
    int i = it.next();
    if (i % 2 == 0) {
        set.remove(i);
    }
}
System.out.println("Size-After: " + set.size());

@mdogan mdogan closed this in fe1a001 Jun 14, 2012

@voodooless

This comment has been minimized.

Copy link
Author

commented Jun 14, 2012

Thanks for the quick answer and fix. I indeed already found the same solution, and it works fine.

eivindw pushed a commit to eivindw/hazelcast that referenced this issue Jun 15, 2012

Fixes hazelcast#189.
(cherry picked from commit fe1a001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.