0

What is the actual order of a HashSet() in java? It is known that HashSet() doesn't have any order. But when I'm executing my code, it is giving same output for every time throughout the week, the code had been executed. This two links (Why HashSet order always same for my program? and Ordering of elements in Java HashSet) gives an idea but not specifies why the output is in same order. My source code is here:

    hs.add("Beta");
    hs.add("Alpha");
    hs.add("Gamma");
    hs.add("jkkl");
    hs.add("c");
    hs.add("F");
    hs.add("Aa");
    hs.add("gAMMa");
    hs.add("d");
    hs.add("null");
    System.out.println(hs);
    System.out.println(hs);
    System.out.println(hs);

The output is:

[Aa, Gamma, jkkl, c, d, null, F, Alpha, gAMMa, Beta]
[Aa, Gamma, jkkl, c, d, null, F, Alpha, gAMMa, Beta]
[Aa, Gamma, jkkl, c, d, null, F, Alpha, gAMMa, Beta]

When I'm putting a instead of Aa and GAMMa instead of gAMMa, then the output is as follows:

[GAMMa, Gamma, a, jkkl, c, d, null, F, Alpha, Beta]
Community
  • 1
  • 1
Samiran Banerjee
  • 23
  • 1
  • 1
  • 8
  • 3
    You're focusing on an irrelevant implementation detail. Add more elements to the set. Change JDK. – Tunaki Mar 10 '16 at 10:58
  • The order is not random, just implementation dependant, so if you insert the same keys in the same order using the same HashMap implementation, you'll get the same order. – Eran Mar 10 '16 at 10:59
  • 1
    " doesn't have any order" That's really all you need to know. The rest are implementation details that you can in no way rely upon. – Daniel Jour Mar 10 '16 at 10:59
  • The duplicate questions you posted answer *your* question too. `GAMMa` has different hash-value than `gAMMa`, that's why it changes order. – radoh Mar 10 '16 at 11:01
  • 1
    The ordering you are getting is side effect of it's implementation. So if the underlying hashing algorithm changes you will get different order. Compare that with ArrayList which always guarantees the order irrespective of implementation. Never rely on implementation side effects only rely on guarantees provided by the data structure. – Adisesha Mar 10 '16 at 11:05
  • @Tunaki, if the HashSet() doesn't have any order, then why the number of element should matter the implementation? Whatever the number of elements are in the HashSet(), it should follow the same rule. – Samiran Banerjee Mar 10 '16 at 11:13
  • @Eran, HashSet() doesn't support the duplicate value also. And what is the actual order followed by HashSet() ? – Samiran Banerjee Mar 10 '16 at 11:25
  • @SamiranBanerjee the same ordering as that of HashMap - again, it's implementation dependent, and depends on the hash codes of the elements added to the HashSet, as well as the order they were added and the size of the HashSet. – Eran Mar 10 '16 at 11:28
  • @DanielJour, Yes this is the only thing. But, is it practically true? If yes then how this implementation is following this? – Samiran Banerjee Mar 10 '16 at 11:30
  • @Eran, ohh ok, I got it. Then it is also shorting the elements by their hash codes, right? – Samiran Banerjee Mar 10 '16 at 11:32
  • Your idea of "no order" seems to be incorrect ... "Whatever the number of elements are in the HashSet(), it should follow the same rule." WHO said that? There are in no particular order. So if for some reason the order of elements gets shuffled at random when you insert another element, then the elements would *still* be in no particular order. Or if they're always shuffled in Mondays, but only if there's not a (insert favourite sport) match going on. – Daniel Jour Mar 10 '16 at 11:33
  • @DanielJour, Yes, may be I'm wrong, but, can you please explain what is the difference between "doesn't have any order" and "no order"? – Samiran Banerjee Mar 10 '16 at 11:49
  • @Adi, can you please suggest how can I improve this implementation? Actually, if I will insert any element, then the order will be changed, that's ok. But, having no particular order, the pre-inserted elements are also not changing. Can you please explain a little bit more? – Samiran Banerjee Mar 10 '16 at 11:54
  • HashSet implements Set. The contract of Set is ensure duplicate elements are not present(Check Java Doc). The order of the Set is undefined. HashSet is free to do whatever it likes as long as the contract of the Set is satisfied. Suppose I am passing a SortedSet to a method which accepts Set as argument. Can it still expect the sorted order you are getting with HashSet now? If you wan't to understand why you are getting this order with HashSet, debug and understand the implementation. Like @Tunaki said, you are wasting your time on irrelevant details. – Adisesha Mar 10 '16 at 17:12

0 Answers0