• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to iterate a Set (2 steps once)

 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can you please tell me if there is any way of iterating over a Set making 2 steps at once?
I would like the equivalent of :



but for a Set collection.

Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's wrong with using an iterator, and just calling next() twice?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course ensuring that there is an even number of elements, or by checking hasNext() before calling next again.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every element in a Set is distinct, so my question is why would you be skipping over elements? And if the elements don't matter why cant you just use an array?

Hunter
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have named your variables correctly, you are not iterating a Set but a List.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"equivalent of"
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:"equivalent of"
Yes, I missed that. But Fred has already given a good answer.
 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you all for your replies. I will call next() twice.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe better to try thisOtherwise you may suffer an exception.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I already said.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:That's what I already said.
Yes, but I only ever notice your posts when they are 5 seconds ahead of mine, not 3 days
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic