This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This set is not removing Objects.

 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic