• 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

Iterator of set

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid Mughal book says that "the order in which the iterator will return its elements from the underlying collection depends on traversal order supported by the collection".

In List, Order supported is "Insertion Order" that would mean that iterator will return elements in sequential order they have in the list.

I am not so clear about how this will work in case of HashSet. As Set do not have any predetermined order. How would iterator determine the order??

Please guide.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set's have an internal ordering. This ordering is not as simple as general sorting. So at some places you will find that sets are not ordered. But internally they also have an order. Iterator for sets will traverse the set in that order...
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Iterator on a LinkedHashmap you will be able to iterate the elements in the exact order, but if you are using a Hashmap the order cannot be guranteed. Thats why it is said that when using an Iterator what matters is the type of Collection you are iterating.

I hope it helps..
 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(A) I have observed that the order in which data is extracted out of HashMap is not guarented.

(B) Also, order in which data is extracted out of hashmap in one request flow is different from the order in which data is extracated out of same hashmap in another request flow.

Seems like HashSet behaves in the same manner. same as (A) and same as (B).

Any views on this
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------------------------------------------------
Set's have an internal ordering. This ordering is not as simple as general sorting. So at some places you will find that sets are not ordered. But internally they also have an order. Iterator for sets will traverse the set in that order...
---------------------------------------------------------------------------

But each time we retrieve the Set we get the output in different order. That means Iterator traverses the sets in different order each time right!!
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But one thing is guaranteed for sure, no node will be traversed more than one time during one iteration.

@Ashu are you absolutely sure that when you traverse a HashSet more than one time without making any changes to it then you can get different ordering each time???
 
reply
    Bookmark Topic Watch Topic
  • New Topic