• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator of set

 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Arun Krishnan Nair
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic