(This question is not about using LinkedHashSet
over HashSet
)
I know that HashSet
does not preserve any order of addition:
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
Suppose there is a program that adds the exactly same elements in the same order to a HashSet
and outputs the elements.
HashSet<Integer> set = new HashSet<Integer>();
Random random = new Random(1000);
for (int i = 0; i < 1000; i++)
set.add(random.nextInt());
set.forEach(s -> System.out.println(s));
If I rerun the same program multiple times, are the outputs guaranteed the same?