HashSet自动排序。

92 阅读1分钟

Java代码

HashSet<Integer> hashSetA = new HashSet<>();
Random random = new Random();
for (int i = 0; i < 12; i++) {
    hashSetA.add(random.nextInt(16));
}
System.out.println(hashSetA);

HashSet<Integer> hashSetB = new HashSet<>();
for (int i = 17; i >= 0; i--) {
    hashSetB.add(random.nextInt(17));
}
System.out.println(hashSetB);

结论:

1、随机生成0-15之间的数字存入hashSetA中,内容进行了排序。

2、随机生成0-16之间的数字存入hashSetB中,内容没有排序。

Snipaste_2023-07-18_10-20-48.png