依据hashmap的原理,自己实现了一个简易版:
public class MyHashMap<K, V> {
private int capacity;
private double loadFactor = 0.8;
private Entry[] table= null;
private int size;
public MyHashMap() {
this(4);
}
public MyHashMap(int capacity) {
this.capacity = capacity;
table = new Entry[capacity];
}
public V put(K key, V value){
if(key == null){
return putForNullKey(value);
}
int hash = getHashCode(key);
int i = indexForKey(hash, capacity);
for (Entry<K,V> e = table[i]; e != null; e = e.next){
Object k = e.getKey();
if(k == key || k.equals(key)){
V oldValue = e.value;
e.value = value;
return oldValue;
}
}
// 需要添加一个entry
addEntry(key, value, i);
return value;
}
public V get(K key){
if(key == null){
return getForNullKey();
}
int index = indexForKey(getHashCode(key), capacity);
Entry entry = table[index];
while(entry != null){
if(entry.getKey() == key){
return (V)entry.getValue();
}
entry = entry.next;
}
return null;
}
public Object remove(K key){
Entry entry = removeEntryForKey(key);
return entry.getValue();
}
public boolean isEmpty(){
return size == 0;
}
public boolean containsKey(K key){
return false;
}
private Entry removeEntryForKey(Object key){
int i = indexForKey(getHashCode(key), table.length);
Entry pre = table[i];
Entry e = pre;
while(e != null){
Entry next = e.next;
Object k;
if( (k = e.key) == key || (key != null && key.equals(k))){
if(pre == e){
table[i] = next;
}else{
pre.next = next;
}
return e;
}
pre = e;
e = e.next;
}
return e;
}
// 保证容量
private void ensureCapacity(){
Entry[] old = table;
int oldCapacity = old.length;
if( size > (loadFactor*oldCapacity)){
capacity = oldCapacity+oldCapacity>>1;
Entry[] newTable = new Entry[capacity];
transform(newTable, true);
table = newTable;
}
}
private void transform(Entry[] newTable, boolean rehash){
int newCapacity = newTable.length;
for(Entry e: table){
while(null != e){
Entry<K,V> next = e.next;
int h = getHashCode(e.getKey());
int i = indexForKey(h, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
private int indexForKey(int hashCode, int length){
return hashCode & (length-1);
}
// 计算对象的hashCode
private int getHashCode(Object object){
return object.hashCode();
}
private V putForNullKey(V value){
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
return oldValue;
}
}
addEntry(null, value, 0);
return value;
}
private V getForNullKey() {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
private void addEntry(K k, V v, int bulkIndex){
ensureCapacity();
Entry e = table[bulkIndex];
Entry ne = new Entry(k, v);;
ne.next = e;
table[bulkIndex] = ne;
size++;
}
public int getSize(){
return size;
}
private static class Entry<K, V>{
private K key;
private V value;
private Entry<K, V> next;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public Entry<K, V> getNext() {
return next;
}
public void setNext(Entry<K, V> next) {
this.next = next;
}
}
}