例子自己写呢,就太麻烦了,给个已经实现的例子你,HashMap的源码主要方法就是你所要的:
static int indexFor(int h, int length) {
return h & (length-1);
}
public Object put(Object key, Object value) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length); for (Entry e = table[i]; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) {
Object oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, k, value, i);
return null;
}
static int hash(Object x) {
int h = x.hashCode(); h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
有不懂的,看看这个Blog,blog.csdn.net/eddygtimegod
我写得比较详细。
static int indexFor(int h, int length) {
return h & (length-1);
}
public Object put(Object key, Object value) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length); for (Entry e = table[i]; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) {
Object oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, k, value, i);
return null;
}
static int hash(Object x) {
int h = x.hashCode(); h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
有不懂的,看看这个Blog,blog.csdn.net/eddygtimegod
我写得比较详细。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货