Obviously, sets do not have any kind of ordering, so I cannot expect any specific ordering if I do
String[] string = mySet.toArray();
However, I am faced with a use case where I don’t care what ordering the string array is in, but I DO need it to be the case that if two sets are equal to each other, then:
StringUtils.join(mySet.toArray(),',');
will produce the same exact string for those sets, always, no matter how many times I run the program assuming I stick with the same code.
Do I have this guarantee?
Similarly, does this whole true for the order that elements appear for a given Set in an iterator?
Technically, no you don’t have a guarantee.
Setis an Interface. Implementations will vary and may or may not obey this requirement.Force the issue by sorting the array yourself after extracting the results from the Set.