JDK源码阅读:实现自己的HashMap

/***
 * 自己实现的一个简单的HashMap<br>
 * 
 * @author jay
 *
 * @param <K>
 * @param <V>
 */
public class MyHashMap<K, V>
{
    private Entry[] table;
    private static int capacity = 16;

    public MyHashMap()
    {
        this(capacity);
    }

    public MyHashMap(int capa)
    {
        table = new Entry[16];
    }

    static class Entry<K, V>
    {
        K key;
        V value;
        Entry next;

        public Entry(K k, V v, Entry ne)
        {
            this.key = k;
            this.value = v;
            this.next = ne;
        }

        public Entry(K k, V v)
        {
            this.key = k;
            this.value = v;
            this.next = null;

        }

        public K getKey()
        {
            return key;
        }

        public void setKey(K key)
        {
            this.key = key;
        }

        public V getValue()
        {
            return value;
        }

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

        public String toString()
        {
            if (next == null)
                return key + "=" + value;
            else
                return key + "=" + value + " -> " + next.toString();
        }

    }

    static final int hash(Object key)
    {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    public V put(K key, V value)
    {
        V oldValue = null;
        int index = (capacity - 1) & hash(key);
        // int index = Math.abs(key.hashCode() % capacity);
        if (table[index] == null)
            table[index] = new Entry<K, V>(key, value);

        Entry<K, V> pair = new Entry(key, value);
        boolean found = false;

        for (Entry e = (Entry) table[index]; e != null; e = e.next)
        {
            Entry<K, V> ipair = e;
            if (ipair.getKey().equals(key))
            {
                oldValue = ipair.getValue();
                e.setValue(value);
                found = true;
                break;
            }
        }

        if (!found)
        {
            Entry entry = (Entry) table[index];
            pair.next = entry;
            table[index] = pair;
        }
        return oldValue;
    }

    public V get(Object key)
    {
        int index = Math.abs(key.hashCode() % capacity);
        if (table[index] == null)
            return null;

        for (Entry e = (Entry) table[index]; e != null; e = e.next)
        {
            if (key.equals(e.getKey()))
                return (V) e.getValue();
        }

        return null;
    }

    public String toString()
    {
        if (table == null || table.length == 0)
            return null;

        StringBuilder builder = new StringBuilder("{\n");
        for (int i = 0; i < table.length; i++)
        {
            Entry e = table[i];
            if (e != null)
            {
                builder.append(e.toString() + "\n");

            }
            // for (Entry e = table[i]; e != null; e = e.next)
        }

        builder.append("}");
        return builder.toString();
    }

    public static void main(String[] ar)
    {
        MyHashMap<Integer, String> map = new MyHashMap<Integer, String>();
        for (int i = 1; i < 96; i++)
            map.put(i, "zhangjie_" + i);
        System.out.println(map.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值