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]