For some tutorial, they said:
HashSet doesn’t maintain any order, the elements would be returned in any random order.
But I write a test program, the result is always same.
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<String> hs1 = new HashSet<String>();
hs1.add("a");
hs1.add("b");
hs1.add("c");
hs1.add("d");
hs1.add(null);
hs1.add(null);
System.out.println(hs1);
System.out.println(hs1);
}
}
Output:
[null, a, b, c, d]
[null, a, b, c, d]
I tried many times, but the order is always same. Why? Hope someone can help me, Thanks in advance!