aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Iterator of set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Iterator of set" Watch "Iterator of set" New topic
Author

Iterator of set

Ashu Jain
Ranch Hand

Joined: Feb 24, 2008
Posts: 73
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arun Krishnan Nair
Greenhorn

Joined: Aug 14, 2008
Posts: 18
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..


SCJP 5 - 100%
Aiming for SCBCD 5 & SCWCD 5
Ashu Jain
Ranch Hand

Joined: Feb 24, 2008
Posts: 73
(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

Joined: May 19, 2006
Posts: 105
----------------------------------------------------------------------------
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!!


If you worry you cannot work... If you work you need not worry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator of set