aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This set is not removing Objects." Watch "This set is not removing Objects." New topic
Author

This set is not removing Objects.

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Hello,

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
while(i.hasNext()){
Object obj= i.next();
log.info("obj instanceof InsertFiles ? " +
(obj instanceof InsertFiles));
if(obj instanceof InsertFiles
|| obj instanceof VectorFiles){
log.info("Removing file : " + obj);
i.remove();
}
}

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...


Regards,

Nitin
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
When I viewed the documentation, it stated the following:
Iterator.remove()

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.

Throws:
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: http://aspose.com/file-tools
 
subject: This set is not removing Objects.
 
Similar Threads
This set is not removing Objects.
Object resolution
Polymorphism question
determine the input type of the value of the function parameter map
TypeFactory can it be done in java?