I am preparing for a UIL computer contest, and one of the practice questions asked involved a set, here it is, word for word:
HashSet<String> set = new HashSet<String>();
set.add("000");
set.add("212");
set.add("211");
set.add("555");
set.add("343");
System.out.println(set);
Now, I understand that HashSets are unsorted data structures, but there were 2 unsorted choices shown, and 1 sorted answer choice:
A) [000, 211, 343, 212, 555]
B) [000, 211, 212, 343, 555]
c) [000, 211, 555, 343, 212]
I naively picked B) the sorted answer, while the correct answer is A, but I still do not understand why. I found the hash code generated by each of the string's hash functions:
"000" - 47664
"212" - 49619
"211" - 49618
"555" - 52629
"343" - 50674
To my knowledge the hash set utilizes a hashtable as its backend. In that case, based on the hash codes, I don't understand why answer is incorrect. I plugged the code into java and it produces the result in the correct answer choice. What is going on here, how exactly does a HashSet add items to itself?