当前位置:   article > 正文

手写hashMap的简单实现_手写hashmap实现

手写hashmap实现

手写hashMap的简单实现

public class MyHashMap<K, V> {
    private static final int DEFAULT_CAPITY = 16;
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;
    private Entry<K, V>[] table;
    private int capity;
    private float loadFactor;
    private int size;
    private int threshold;

    public MyHashMap() {
        this.capity = DEFAULT_CAPITY;
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        this.threshold = (int) (DEFAULT_CAPITY * DEFAULT_LOAD_FACTOR);
    }

    public MyHashMap(int capity) {
        this.capity = tableSizeFor(capity);
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        this.threshold = (int) (this.capity * this.loadFactor);
    }
    public void put (K key, V value) {
        if (null == table) {
            table = new Entry[capity];
        }
        int hashCode = hash(key);
        int index = indexFor(hashCode);
        for (Entry<K, V> entry = table[index]; entry != null; entry = entry.next) {
            if (entry.key.equals(key)) {
                entry.value = value;
                return;
            }
        }
        addEntry(hashCode, key, value, index);
    }
    public V get(K key) {
        int hashCode = hash(key);
        int index = indexFor(hashCode);
        for (Entry<K, V> entry = table[index]; entry != null; entry = entry.next) {
            if (entry.key.equals(key)) {
                return entry.value;
            }
        }
        return null;
    }
    private void addEntry(int hash, K key, V value, int index) {
        Entry<K, V> entry = new Entry<>(key, value, hash, table[index]);
        table[index] = entry;
        ++size;
        if (size > threshold) {
            resize();
        }
    }
    private void resize() {
        Entry<K, V>[] oldTable = table;
        int oldCap = oldTable.length;
        capity = capity << 1;
        threshold = threshold << 1;
        table = new Entry[capity];
        for (int i = 0; i < oldCap; ++i) {
            Entry<K, V> entry;
            if ((entry = oldTable[i]).next == null) {
                table[entry.hash & (capity - 1)] = entry;
            } else {
                Entry<K, V> loHead = null, loTail = null;
                Entry<K, V> hiHead = null, hiTail = null;
                Entry<K, V> next;
                do {
                    next = entry.next;
                    if ((entry.hash & oldCap) == 0) {
                        if (null == loHead) {
                            loHead = entry;
                        } else {
                            loTail.next = entry;
                        }
                        loTail = entry;
                    } else {
                        if (null == hiHead) {
                            hiHead = entry;
                        } else {
                            hiTail.next = entry;
                        }
                        hiTail = entry;
                    }
                } while ((entry = next) != null);
                if (null != loTail) {
                    table[i] = loHead;
                }
                if (null != hiTail) {
                    table[i + oldCap] = hiHead;
                }
            }
        }
    }
    
    private int indexFor(int hashCode) {
        return hashCode & (capity - 1);
    }
    
    private int hash(K key) {
        if (null == key) {
            return 0;
        }
        int h = key.hashCode();
        return h ^ (h >> 16);
    }

    private int tableSizeFor(int capity) {
        int n = capity - 1;
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
        return n < 0 ? DEFAULT_CAPITY : (n + 1);
    }

    class Entry<K, V> {
        private K key;
        private V value;
        private int hash;
        private Entry<K, V> next;

        public Entry(K key, V value, int hash, Entry<K, V> next) {
            this.key = key;
            this.value = value;
            this.hash = hash;
            this.next = next;
        }
    }
    public static void main(String[] args) {
        MyHashMap<String, String> myhashMap = new MyHashMap<>(4);
        myhashMap.put("1", "1v");
        myhashMap.put("2", "2v");
        myhashMap.put("3", "3v");
        myhashMap.put("4", "4v");
        myhashMap.put("5", "5v");
        myhashMap.put("5", "6v");
        System.out.println(myhashMap.get("5"));
        System.out.println("capity : " + myhashMap.capity);
        System.out.println("size : " + myhashMap.size);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/227364
推荐阅读
相关标签
  

闽ICP备14008679号