private static Random rand = new Random();
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
for (int i = 0; i < 1000; i++) {
Integer r = new Integer(rand.nextInt(20));
if (hm.containsKey(r)) { }
else { hm.put(r,Integer.toString(i)); }
}
System.out.println(hm.toString()); }
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
for (int i = 0; i < 1000; i++) {
Integer r = new Integer(rand.nextInt(20));
if (hm.containsKey(r)) { }
else { hm.put(r,Integer.toString(i)); }
}
System.out.println(hm.toString()); }
本文探讨了HashMap在Java中的实现原理,特别是其内部如何通过特定的哈希算法对元素进行存储,解释了为何输出时会呈现出一种看似有序的现象。


1699

被折叠的 条评论
为什么被折叠?



