• 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

Iteration order of Hash Set

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain to me this: there is no guarantee that the iterator of a hash set return the elements in the set in particular order and there is no guarantee the order remain consistent over time?
Does that mean given a piece of code with a hash set of elements being added, I cannot predicate the iteration order?

I mistakenly thought that the iteration order is based on the hash code. The element with the smallest hash code is iterated first and so on and the element with the largest hash code is iterated last. But I experiment it, it is not true.


 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, in a hash set, the order of iteration is not in any particular order. Hash set is mainly used for better performance in element access. So the more unique the hash code for elements in a collection is, the better is the performance. Hope you have a good understanding of how hashing works.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A specific implementation of HashSet might have a predictable order...but it doesn't have to, and there are no guarantees that they won't change it in the future even if it does now. All you can and should rely on is the contract, which says the order is not predictable.

If you want a set with a predictable order, you should look at LinkedHashSet (for first-in-first-out) or TreeSet (for a sorted set).
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One possibility from my understanding is that a very specific implementation of hashcode could do the job. But when we have linkedhash collections for this purpose, better use the available source.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larsen Raja wrote:One possibility from my understanding is that a very specific implementation of hashcode could do the job.


Only if you knew the exact implementation - and then depending on the implementation possibly not even then.
 
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element."

http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html


I think the key to understand this concept is knowing the difference between sorted and ordered. HashSet is unsorted and unordered.
 
reply
    Bookmark Topic Watch Topic
  • New Topic