public interface MyMap<K, V> {
public V put(K k, V v);
public V get(K k);
interface Entry<K, V>{
public K getKey();
public V getValue();
}
}
2. 主要部分
import java.util.ArrayList;
import java.util.List;
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 int defaultInitSize;
private float defaultLoadFactor;
//Map中Entry的数量
private int entryUserSize;
//数组
private Entry<K,V>[] table = null;
//MyHashMap的构造
public MyHashMap() {
this.defaultLoadFactor = DEFAULT_LOAD_FACTOR;
this.defaultInitSize = DEFAULT_INITIAL_CAPACITY;
table = new Entry[this.defaultInitSize];
}
public MyHashMap(int defaultInitSize, int defaultLoadFactor){
/*if(defaultLoadFactor < 0){
throw new IllegalArgumentException("Illegal initial capacity:" + defaultInitSize);
}
if(defaultLoadFactor <= 0 || Float.isNaN((defaultLoadFactor))){
throw new IllegalArgumentException("Illegal load factor:"+ defaultLoadFactor);
}*/
this.defaultInitSize = defaultInitSize;
this.defaultLoadFactor = defaultLoadFactor;
table = new Entry[this.defaultInitSize];
}
class Entry<K,V> implements MyMap.Entry<K,V>{//内部类
private K key;
private V value;
private Entry<K,V> next;
public Entry(){ }
public Entry(K key, V value, Entry<K,V> next){
this.key = key;
this.value = value;
this.next = next;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
}
@Override
public V put(K k, V v) {
V oldValue = null;
//是否需要扩容
//扩容完毕 肯定要重新散列
if(entryUserSize >= defaultInitSize * defaultLoadFactor){
resize(2 * defaultInitSize);
}
//得到Hash值,计算出数组中的位置
int index = hash(k) & (defaultInitSize-1);
if(table[index] == null){
table[index] = new Entry<K,V>(k,v,null);
++entryUserSize;
}else{//需要遍历单链表
Entry<K,V> entry = table[index];
Entry<K,V> e = entry;
while(e != null){
if(k == e.getKey() || k.equals(e.getKey())){
oldValue = e.value;
e.value = v;
return oldValue;//更新完返回
}
e = e.next;
}
table[index] = new Entry<K,V>(k,v,entry);//头插法
++entryUserSize;
}
return oldValue;
}
private int hash(K k){
int h;
return (k == null) ? 0 : Math.abs(h = k.hashCode()) ^ (h >>> 16);
}
private void resize(int i){
Entry[] newTable = new Entry[i];
defaultInitSize = i;
entryUserSize = 0;
rehash(newTable);
}
private void rehash(Entry<K,V>[] newTable){
//得到了原来老的Entry集合 注意遍历单链表
List<Entry<K,V>> entryList = new ArrayList<>();
for(Entry<K,V> entry : table){
if(entry != null){
while(entry != null){
entryList.add(entry);
entry = entry.next;
}
}
}
//覆盖旧的引用
if(newTable.length > 0){
table = newTable;
}
//所谓重新Hash,就是重新put Entry到HashMap
for(Entry<K,V> entry : entryList){
put(entry.getKey(), entry.getValue());
}
}
@Override
public V get(K k) {
int index = hash(k) & (defaultInitSize-1);
if(table[index] == null){
return null;
}else{
Entry<K,V> entry = table[index];
while(entry != null){
if(k == entry.getKey() || k.equals(entry.getKey())){
return entry.getValue();
}
entry = entry.next;
}
}
return null;
}
}
3. 测试部分
public class Test {
public static void main(String[] args) {
MyMap<String,String> myMap = new MyHashMap<>();
for(int i = 0; i < 200; i++){
myMap.put("key"+i,"value"+i);
}
for(int i = 0; i < 200; i++){
System.out.println("key"+i+"value is : " + myMap.get("key"+i));
}
}
}