易错点合集

1.PriorityQueue的方法iterator()中提供的迭代器并不保证以有序的方式遍历优先级队列中的元素,因此不能使用此迭代器依次遍历PriorityQueue中的队头元素。

2. hash函数避免冲突的经验值取大质数(如果capacity比较大的话)。比如如下的冲突避免hash function。

 1     private int getHash(int[] count) {
 2         int hash = 0;
 3         int a = 378551;
 4         int b = 63689;
 5         for (int num : count) {
 6             hash = hash * a + num;
 7             a = a * b;
 8         }
 9         return hash;
10     }
View Code

 附一张big prime number table

lwrupr% errprime
25 26 10.416667 53
26 27 1.041667 97
27 28 0.520833 193
28 29 1.302083 389
29 210 0.130208 769
210 211 0.455729 1543
211 212 0.227865 3079
212 213 0.113932 6151
213 214 0.008138 12289
214 215 0.069173 24593
215 216 0.010173 49157
216 217 0.013224 98317
217 218 0.002543 196613
218 219 0.006358 393241
219 220 0.000127 786433
220 221 0.000318 1572869
221 222 0.000350 3145739
222 223 0.000207 6291469
223 224 0.000040 12582917
224 225 0.000075 25165843
225 226 0.000010 50331653
226 227 0.000023 100663319
227 228 0.000009 201326611
228 229 0.000001 402653189
229 230 0.000011 805306457
230 231 0.000000 1610612741

3. 判断两个Integer对象相等时一律用equals方法,只有Integer的值在-128 - 127才不会new Integer,直接==比较不会出错。超出这个范围比较的就是内存地址。

4.Iterator中的next方法返回的是Object对象,所以一定要记得强制转换成原来的类型。比如(Integer) it.next()。

原文地址:https://www.cnblogs.com/choumeng/p/6445748.html