hashmap的简单实现

/**
 * @author luxiangxing
 * @time   2017-05-05
 * @email  [email protected]
 * @tel    15330078427
 */
public class SimpleHashMap<K,V> {
	private int size = 100;
	private Entry<K,V>[] table = new Entry[size];
	
	public void put(K k,V v){
		int h = hash(k);
		Entry o = table[h];
		table[h] = new Entry(h,k,v,o);
	}
	
	public V get(K k){
		int h = hash(k);
		Entry<K,V> c = table[h];
		while(c!=null){
			if (c.k.equals(k)) {
				return c.v;
			}
			c = c.e;
		}
		return null;
	}
	
	
        private int hash(K key) {
            return (key.hashCode() & 0x7fffffff) % size;
        }
}

class Entry<K,V>{
	private int h;
	K k;
	V v;
	Entry e;
	public Entry(int h,K k,V v,Entry e) {
		this.h = h;
		this.k = k;
		this.v = v;
		this.e = e;
	}
}

猜你喜欢

转载自xiangxingchina.iteye.com/blog/2372980