I found a (for me) inexplicable behavior with an implemented HashSet in Java. I implemented the HashSet like this and filled it with the values of a list.
HashSet<Integer> set = new HashSet<Integer>(list);
I first used a list containing numbers reaching fom 0 to 9 to fill the HashSet:
Example: {1,0,5,9,6,7,3,1,3,6,1,5,1,3,4,9,9,7}
Output: [0, 1, 3, 4, 5, 6, 7, 9]
Since HashSets normally give back values in an ascending sorted order until now everything worked fine. But as soon as I started using a list containing bigger vaues it starts giving back the values in a weird way:
Example: {67,1,122,19,456,42,144,42,3,34,5,5,42}
Output: [1, 34, 67, 3, 5, 456, 42, 144, 19, 122]
I read something about that this depends on the internal hashing algorithm here: Java HashSet shows list in weird order, always starting with 3 but that is even more confusing since I used the exact same HashSet just with different values.
Could please somebody explain me why this is happening?