HashMap简易版

转载https://www.cnblogs.com/vitalq/p/9615659.html

public interface BRX_MAP<K,V> {

    V put(K key, V value);

    V get(K key);

    Integer size();

    interface Entry<K,V> {
        K getKey();
        V getVaule();
    }
}
public class BHashMap<K,V> implements BRX_MAP<K,V> {

    public static void main(String[] args) {
        BHashMap<String,String> hashMap = new BHashMap<>();
        hashMap.put("name","brx");
        String name = hashMap.get("name");
        System.out.println(name);
        hashMap.put("name","hqs");
        name = hashMap.get("name");
        System.out.println(name);
    }

    //存储数据的数组 ,每一个Entry是一个链表
    private Entry<K, V>[] table;

    private Integer size = 0;

    //默认大小
    private Integer defaultLength = 16;

    //负载因子
    private double loadFactor = 0.75D;

    BHashMap() {
        this.table = new Entry[defaultLength];
    }

    Integer getIndex(K key) {
        int index = Math.abs((key.hashCode() % (defaultLength - 1)));
        return index;
    }

    @Override
    public V put(K key, V value) {
        Integer index = getIndex(key);
        System.out.println("INDEX:"+index);
        //扩容
        if (table.length <= index + 1) {
            Integer len = table.length + (int) (table.length * loadFactor);
            Entry<K, V>[] newTable = Arrays.copyOf(table, len);
            this.table = newTable;
        }

        //判断下标是否被占用
        Entry<K, V> kvEntry = table[index];
        //没有被占用
        if (kvEntry == null) {
            table[index] = new Entry(key, value, null, index);
            size++;
        } else {
            //判断是否相同的key
            if (kvEntry.key.equals(key)) {
                //覆盖相同的key对应的value值
                table[index] = new Entry(key, value, kvEntry.next, index);
            } else {
                //把新的值放进去   新进元素,从链表的头部插入(头插法)
                table[index] = new Entry(key, value, kvEntry, index);
                size++;
            }
        }
        return table[index].getVaule();

    }

    @Override
    public V get(K key) {
        Integer index = getIndex(key); 
        Entry<K, V> kvEntry = table[index];//先从数组里找到对应的entry,再从entry里循环遍历链表,equals(key)
        do {
            if (kvEntry == null) {
                return null;
            }
            if (kvEntry.key.equals(key)) {
                return table[index].getVaule();
            }
            kvEntry = kvEntry.next;
        } while (true);

    }


    @Override
    public Integer size() {
        return size;
    }

    class Entry<K, V> implements BRX_MAP.Entry<K, V> {

        private Entry<K, V> next;
        private Integer index;
        private K key;
        private V value;

        Entry(K _key, V _value, Entry<K, V> _next, Integer _index) {
            this.key = _key;
            this.value = _value;
            this.next = _next;
            this.index = _index;
        }

        Entry() {
        }

        @Override
        public K getKey() {
            return this.key;
        }

        @Override
        public V getVaule() {
            return this.value;
        }


    }
}
原文地址:https://www.cnblogs.com/brxHqs/p/11691685.html