• Post Reply Bookmark Topic Watch Topic
  • New Topic

remove from Set  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this to remove a object:



This works, but I wonder why I do not need an iterator for that.
I have thought, removing a object in this case needs an iterator., but it works in this case. Why?

(MyClass.getMyObjects() is a static Set)

However, should I use the iterator even if it works? Is it faster with an iterator?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does getMyObjects return a new Set each time, perhaps? Because if I look at this code, I really expect a ConcurrentModificationException. Usually, you should use an Iterator when removing from the collection you iterate over.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know, why this works. It s a static Set, getMyObjects return not a new Set each loop.

I would expect also the ConcurrentModificationException, but it works. a returns true:


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:I do not know, why this works.
I would expect also the ConcurrentModificationException, but it works. a returns true:




How many elements are there in MyClass.getMyObjects() . i guess less than 3 .
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THis is an enhanced for loop right? Enhanced for loops start at the minimum range(usually 0) and go through the max range you set (so in this case MyClass.getMyObjects() ) im assuming that method returns more than one object.


Hunter.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I had only one element in the list.

When I have to change a object within a loop by getter/setter, do I need the Iterator, too? Or do I need it only when removing objects from the Set?
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sets don't have get and set methods, because they do not have a notion of the position of elements. Only Lists have a notion of position, so only Lists have get and set methods.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:yes, I had only one element in the list.

Then that explains it. The call to remove is done when you're actually done iterating.

When I have to change a object within a loop by getter/setter, do I need the Iterator, too? Or do I need it only when removing objects from the Set?

You can't add using an Iterator (only with ListIterator), so removing is all you can do with it. If you need to add while iterating you will need a copy of the Set, and add to that.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean the Object within the Iteration:

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That won't be a problem. The object is an element of the Set, but it isn't actually part of the iteration process. You just use it to store the current element temporarily.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!