Java 自己实现一个简单的HashMap

package com.yaphetzhao.test;

/**
 * Created by YaphetZhao 
 * on 2017/2/16.
 */
public class YapHashMap {

    public static void main(String[] args) {
        YapHashMap hashMap = new YapHashMap<>();
        hashMap.insert("1", "value_1");
        System.out.println(hashMap.size);
    }

    private Node[] buckets;
    private static double A = (Math.pow(5, 0.5) - 1) / 2;
    private int capacity;
    private int size = 0;

    private YapHashMap() {
        int temp = 1;
        while (temp < 16) {
            temp <<= 2;
            System.out.println(temp);
        }
        this.capacity = temp;
        buckets = new Node[this.capacity];
    }

    private int index(K key) {
        int hashCode = key.hashCode();

        double temp = hashCode * A;
        double digit = temp - Math.floor(temp);

        return (int) Math.floor(digit * capacity);
    }

    public void insert(K key, V value) {
        int position = index(key);
        Node node = new Node(key, value);
        if (buckets[position] != null) {
            node.setNext(buckets[position]);
        }
        buckets[position] = node;
        size++;
    }

    public void delete(K key) {
        int position = index(key);
        Node node = buckets[position];
        if (node == null) {
            return;
        }
        if (node.key.equals(key)) {
            buckets[position] = node.next;
            size--;
        }
        while (node.next != null) {
            if (node.next.key.equals(key)) {
                node.next = node.next.next;
                size--;
                break;
            }
            node = node.next;
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    static class Node {
        private final K key;
        private V value;
        private Node next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        public K getKey() {
            return key;
        }
    }

}


你可能感兴趣的:(Java 自己实现一个简单的HashMap)