实现简易Hashmap代码

package HashMap和LinkedHashMap;

/**
 * @author Dracular
 * @version $Rev$
 * @des ${TODO}
 * @date 2019/1/23 下午4:33
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class myHashMap {

    /**
     * 构造函数
     *
     * @param 
     * @param 
     */
    private class Entry {

        int hash;
        Key key;
        Value value;
        Entry next;

        /**
         * 键值对
         *
         * @param hash  哈希值
         * @param key   键
         * @param value 值
         * @param next
         */
        private Entry(int hash, Key key, Value value, Entry next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 默认容量
     * 容量必须是2的幂
     */
    public static final int DEFAULT_CAPACITY = 1 << 4;

    /**
     * 默认Entry数组
     */
    private Entry[] table;

    /**
     * 数组容量
     */
    private int capacity;

    /**
     * 数组实际大小
     */
    private int size;

    public myHashMap() {
        this(DEFAULT_CAPACITY);
    }

    public myHashMap(int capacity) {
        if (capacity < 0) {
            throw new IllegalArgumentException();
        } else {
            table = new Entry[capacity];
            size = 0;
            this.capacity = capacity;
        }
    }

    /**
     * 获取数组大小
     *
     * @return
     */
    public int size() {
        return size;
    }

    /**
     * 判断数组是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0 ? true : false;

    }

    /**
     * 得到哈希值
     *
     * @param key
     * @return
     */
    public int hash(Key key) {
        double tmp = key.hashCode() * (Math.pow(5, 0.5) - 1) / 2;
        double digit = tmp - Math.floor(tmp);
        return (int) Math.floor(digit * capacity);
    }

    /**
     * put方法
     * 存放键值对
     *
     * @param key   键
     * @param value 值
     */
    public void put(Key key, Value value) {
        if (key == null) {
            throw new IllegalArgumentException();
        }
        int hash = hash(key);
        Entry nEntry = new Entry<>(hash, key, value, null);
        Entry entry = table[hash];
        while (entry != null) {
            if (entry.key.equals(key)) {
                entry.value = value;
            }
            entry = entry.next;
        }
        nEntry.next = table[hash];
        table[hash] = nEntry;
        size++;
    }

    /**
     * get方法
     * 得道对应key的值
     *
     * @param key
     * @return
     */
    public Value get(Key key) {
        if (key == null) {
            throw new IllegalArgumentException();
        }
        int hash = hash(key);
        Entry entry = table[hash];
        while (entry != null) {
            if (entry.key.equals(key)) {
                return entry.value;
            }
            entry = entry.next;
        }
        return null;
    }

    public static void main(String[] args) {
        myHashMap map = new myHashMap<>();
        map.put("陈旭宇", "沙雕");
        System.out.println(map.get("陈旭宇"));
    }
}

输出结果:

沙雕

你可能感兴趣的:(算法小菜)