• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieval Order of Iterator Interface  RSS feed

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the retrieval order of Iterator. I mean iterator iterates Inserted order or reverse order.
If i want to change the order means, what i have to do
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends entirely on the collection you're iterating over. Some collections have a defined order (for example a List), some are not ordered (for example a HashSet), so the order of iteration is undefined.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that undefined does not mean random. The order will most likely be the same if you're iterating twice in a row without modifying the collection.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The order will most likely be the same if you're iterating twice in a row without modifying the collection.

Yes, but there's no guarantee that that's always so, so don't write your program to rely on that fact.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There always is some order an Iterator iterates on ...

the only thing is that sometimes its known as in case of a linked list ... and sometimes it's not as in case of a hashmap ...

as haspmaps store elements using hashing while in a linked list the iteration is simple ... using the links only ..
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!