最近看到一个面试题:手写HashMap,第一次写这个东西,觉得无从下手,上网copy了很多代码,复杂是复杂,就是有错,put进去的东西,并不能get出来,无奈之下自己写,以下是实现了简单的get()和put()方法,先上传,下次把扩容等功能补上。
写之前最好先理解一下原理,可以少走弯路,先新建16个桶即bucket(16是源码默认的,可以自定义),给桶分别标上1~16标号,put一个键值对之后,用key的hash值对16取余,结果肯定也是一个1~16范围内的数值,把这对键值对放到取余后的值对应的那个的桶里(注意,不可能为每一个hash值都创建一个桶,那样的话代价太大,这里只创建了16个桶,所以很有可能一个桶里放入多个键值对;如果偏巧那个桶里是空的,直接把key、value放进去ok的,如果桶不为空,就要一个一个比对桶里原有的key是否和现在要放进去的key是同一个(即hash值相同且equals为true),如果是同一个,那么就用新的value覆盖替换原来的value就行,如果遍历完了,没有一个相同的key,那么就放到所有key的最后面(据说jdk1.8之后是放在最前面,具体还没有研究到),这样有多个键值对的桶里就形成了一个链表结构,而多个桶之间是数组元素的关系,所以说hashmap就是数组+链表结构。
public interface MyMap<K, V> {
V put(K key, V value);
V get(K key);
interface Entry<K, V> {
K getKey();
V getValue();
}
}
public class MyHashMap<K, V> implements MyMap<K, V> {
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private float loadFactor = 0;
private int initCapacity = 0;
private Entry<K, V>[] table = null;
public MyHashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
this.initCapacity = DEFAULT_INITIAL_CAPACITY;
table = new Entry[this.initCapacity];
}
public MyHashMap(int initCapacity, float loadFactor) {
this.loadFactor = loadFactor;
this.initCapacity = initCapacity;
table = new Entry[this.initCapacity];
}
private int hash(K key) {
int h;
return (key == null) ? 0 : Math.abs((h = key.hashCode())) ^ (h >>> 16);
}
@Override
public V put(K key, V value) {
int index = hash(key) % initCapacity;
if (table[index] != null) {
Entry<K, V> e = table[index];
Entry<K, V> e2 = null;
while (e != null) {
if (hash(e.key) == hash(key) && e.key.equals(key)) {
e.value = value;
return value;
}
e2 = e;
e = e.next;
}
e2.next = new Entry<>(key, value, null, index);
} else {
Entry<K, V> e = new Entry<>(key, value, null, index);
table[index] = e;
}
return value;
}
@Override
public V get(K key) {
int index = hash(key) % initCapacity;
Entry<K, V> e = table[index];
if (e == null) {
return null;
}
while (e != null) {
if (e.key == null && key == null ||
hash(e.key) == hash(key) && e.key.equals(key)) {
return e.value;
}
e = e.next;
}
return null;
}
class Entry<K, V> implements MyMap.Entry<K, V> {
K key;
V value;
Entry<K, V> next;
int index;//记录下标
Entry(K k, V v, Entry<K, V> next, int inx) {
this.key = k;
this.value = v;
this.index = inx;
this.next = next;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public Entry getNext() {
return next;
}
}
}
public class Test {
public static void main(String[] args) {
// Map<String, String> map = new HashMap();
// map.put("name","sophia");
// map.put("age", "18");
// System.out.println(map.get("name"));
// System.out.println(map.get("age"));
MyMap<String, Object> map = new MyHashMap<>();
map.put("name", "sophia");
map.put("age", 18);
map.put("hahaha", "hehehe");
map.put("hehehe", "lalala");
map.put(null, "lalala");
System.out.println(map.get("name"));
System.out.println(map.get("age"));
System.out.println(map.get("hahaha"));
System.out.println(map.get("hehehe"));
System.out.println(map.get(null));
}
}