我遇到了一个有趣的话题:为什么HashSet类将1到9位数字从小到大排序,而其余的数字却没有升序排序。我知道HashSet专注于速度的想法。但是,如果有人可以解释为什么仅对1到9的数字进行排序,我将不胜感激。
例子:
public static void main(String[] args) {
int count[] = {3, 2, 3, 7, 6};
Set<Integer> set = new HashSet<>();
for (int i = 0; i < 5; i++) {
set.add(count[i]);
}
System.out.println(set);
}
Output: [2, 3, 6, 7]
在int count[] = {44,32,87,32,11};
输出的情况下:[32、87、11、44]
a上的迭代顺序HashSet
将取决于三个因素:
Integer#hashCode()
HashSet
观察到的行为只是一个巧合。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句