File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes This set is not removing Objects. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This set is not removing Objects." Watch "This set is not removing Objects." New topic

This set is not removing Objects.

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126


Files is the superclass and I have a set of subclasses like InsertFiles, VectorFiles. Following is the code to clear the matching objects.

Iterator i = filesSet.iterator();
// Clear the old Files in filesSet
Object obj=;"obj instanceof InsertFiles ? " +
(obj instanceof InsertFiles));
if(obj instanceof InsertFiles
|| obj instanceof VectorFiles){"Removing file : " + obj);

I have overloaded the equals(), toString() and hashCode() method of subclasses to compare all the properties so that the objects can remain unique for a Set. I can add up any number of objects to the set. But when I try to remove using the Iterator the object is not removed. What might be the reason ? What does iterator.remove() internally does with the objects ? Anyone having any idea ?

Of course this is just a small portion of the code that I have written.

Hope I have made the things clear.. waiting for replies...


Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
When I viewed the documentation, it stated the following:

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

UnsupportedOperationException - if the remove operation is not supported by this Iterator.
IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method.

Do you have any error handling that would catch this error further up in the code hierarchy? Has the collection been modified prior to the removal phase of the code?
I agree. Here's the link:
subject: This set is not removing Objects.
jQuery in Action, 3rd edition