HashMap的简单实现

依据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;
        }
    }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值