簡易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("   "));
    }
}

出力結果:


砂の彫刻