HashMap在使用int为KEY时,会自动装箱,变成Integer。有时是好事,有时让人非常气愤。原因就是产生大量的Integer,使得性能急剧下降。那么能不能直接使用int作为KEY呢?
最低要求是放入时,无新对象产生(或者是利用已有的)。否则就失去了意义。比如有的在put时直接产生一个对象,大哥你这个比人家Integer有什么好处?速度快一点?
网上共搜索到4个,分别点评:
- https://github.com/jglick/inthashmap
put时无新对象产生,合格。
- https://github.com/aikar/inthashmap
put时无新对象产生,合格。
- https://github.com/YuliProgrammer/IntHashMap
put时有新对象产生,不合格。
- 这个搜索到的一个源码
put时有新对象产生,不合格。
package taishan;
/**
* @author 杜科
* @description 定制化的HashMap * 简单测试
* 当量级为百万级时,比起HashMap put提升超8倍,get提升超10倍。似乎此时最佳
* 当量级增大到千万级时,put的性能比HashMap持平,甚至稍弱,get的性能为HashMap 4倍
* @contact AllenDuke@163.com
* @date 2020/7/16
*/
public class IntHashMap {
private class IntEntry {
int key;
V value;
IntEntry next;
}
private IntEntry[] table;
private int capacity;//2的n次方
public IntHashMap(int capacity) {
if(!checkCapacity(capacity)) throw new RuntimeException("capacity:"+capacity+",不是2的n次方");
this.capacity = capacity;
table = new IntEntry[capacity];
}
//检查容量是否为2的n次方 按位检查1的个数
public static boolean checkCapacity(int capacity) {
int oneCount = 0;
for (int i = 0; i < 32; i++) {
if ((capacity & 1) == 1) oneCount++;
capacity >>>= 1;
}
if (oneCount == 1) return true;
else return false;
}
//性能比checkCapacity稍好
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
//todo 更为更优秀的hash函数
private static int indexFor(int key, int length) {
return key & (length - 1);
}
public void put(int key, V value) {
IntEntry entry = new IntEntry();
entry.key = key;
entry.value = value;
int i = indexFor(key, table.length);
IntEntry head = table[i];
if (head == null) {
table[i] = entry;
return;
}
while (head.next != null) head = head.next;
head.next = entry;
}
public V get(int key) {
int i = indexFor(key, table.length);
IntEntry head = table[i];
while (true) {
if (head == null) return null;//不做检查使用者保证,减少运算
if (head.key == key) return head.value;
head = head.next;
}
}
}