• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How to iterate a Set (2 steps once)

 
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
 
lowercase baba
Posts: 12933
65
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?
 
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • 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
 
Marshal
Posts: 70998
291
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"equivalent of"
 
Campbell Ritchie
Marshal
Posts: 70998
291
  • 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
Marshal
Posts: 70998
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe better to try thisOtherwise you may suffer an exception.
 
Rob Spoor
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I already said.
 
Campbell Ritchie
Marshal
Posts: 70998
291
  • 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
 
You have to be odd to be #1 - Seuss. An odd little ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic