• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove from Set

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean the Object within the Iteration:

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic