So what I know is that a HashSet has no real sorting capabilities like a SortedSet, however I stumbled upon this :
When I run the following code :
public static void main(String[] args) {
Set<String> collection = new HashSet<String>(2000);
String[] data = {"a", "c", "g", "f", "b", "f", "b", "d","q","r","d","m"};
for(String input: data)
{
collection.add(input);
}
System.out.println("Output: " + collection);
}
I get the following output : Output: [a, b, c, d, f, g, m, q, r]
Which is alphabetically sorted. Why is that? Since a HashSet is not a sorted set.
So I tried with a string of characters instead of a single character :
public static void main(String[] args) {
Set<String> collection = new HashSet<String>(2000);
String[] data = {"atjre", "crj", "gertj", "fertj", "berj"};
for(String input: data)
{
collection.add(input);
}
System.out.println("Output: " + collection);
}
And i get the following output : Output: [crj, atjre, fertj, gertj, berj]
Now they are not sorted anymore, any explanations for this? Or is this just a random coincidence?