C#实现HashMap类

背景我的同()事(),这个逼以前是干Java开发的,想要在C#里面使用HashMap,我一开始以为C#有,结果怎么都引用不出来,于是百度了,还是没找到,由于HashMap和Dictionary的功能很像,都是采用数组链表,然后我就自己继承IDictionary接口添加字典的实现方法,在类中定义一个Dictionary,改写部分方法,自己实现了一个HashMap类

IDictionary接口

需要实现的属性

  • int Count
  • bool IsReadOnly
  • ICollection<TKey> Keys
  • ICollection<TValue> Values

需要实现的方法

  • void Add(KeyValuePair<TKey, TValue> item)
  • void Add(TKey key, TValue value)
  • void Clear()
  • bool Contains(KeyValuePair<TKey, TValue> item)
  • bool ContainsKey(TKey key)
  • void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  • IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  • bool Remove(KeyValuePair<TKey, TValue> item)
  • bool Remove(TKey key)
  • bool TryGetValue(TKey key, out TValue value)
  • IEnumerator IEnumerable.GetEnumerator()

比较好玩的就是这个IsReadOnly属性,微软说,直接返回false

HashMap

public class HashMap<TKey, TValue> : IDictionary<TKey, TValue>, IEnumerable
{
    Dictionary<TKey, TValue> HashMapData = new Dictionary<TKey, TValue>();
    public TValue this[TKey key]
    {
        get
        {
            return HashMapData[key];
        }

        set
        {
            if (HashMapData.ContainsKey(key))
            {
                HashMapData[key] = value;
            }
            else
            {
                HashMapData.Add(key, value);
            }
        }
    }

    public int Count
    {
        get
        {
            return HashMapData.Count;
        }
    }

    //如果 true 是只读的,则为 IDictionary;否则为 false。 在 Dictionary<TKey,TValue> 的默认实现中,此属性始终返回 false。
    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public ICollection<TKey> Keys
    {
        get
        {
            return HashMapData.Keys;
        }
    }

    public ICollection<TValue> Values
    {
        get
        {
            return HashMapData.Values;
        }
    }

    public void Add(KeyValuePair<TKey, TValue> item)
    {
        if (item.Key == null)
        {
            throw new ArgumentNullException("key can't be null.");
        }
        if (HashMapData.ContainsKey(item.Key))
        {
            HashMapData[item.Key] = item.Value;
        }
        else
        {
            HashMapData.Add(item.Key, item.Value);
        }
    }

    public void Add(TKey key, TValue value)
    {
        if (key == null)
        {
            throw new ArgumentNullException("key can't be null.");
        }
        if (HashMapData.ContainsKey(key))
        {
            HashMapData[key] = value;
        }
        else
        {
            HashMapData.Add(key, value);
        }
    }

    public void Clear()
    {
        HashMapData.Clear();
    }

    public bool Contains(KeyValuePair<TKey, TValue> item)
    {
        return HashMapData.Contains(item);
    }

    public bool ContainsKey(TKey key)
    {
        return HashMapData.ContainsKey(key);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        if (array == null)
        {
            return;
        }
        if (array.Length <= arrayIndex)
        {
            return;
        }
        int count = array.Length;
        for (int i = 0; i < count; i++)
        {
            if (HashMapData.ContainsKey(array[i].Key))
            {
                HashMapData[array[i].Key] = array[i].Value;
            }
            else
            {
                HashMapData.Add(array[i].Key, array[i].Value);
            }
        }
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return HashMapData.GetEnumerator();
    }

    public bool Remove(KeyValuePair<TKey, TValue> item)
    {
        if (HashMapData.ContainsKey(item.Key) && HashMapData.ContainsValue(item.Value))
        {
            return HashMapData.Remove(item.Key);
        }
        return false;
    }

    public bool Remove(TKey key)
    {
        return HashMapData.Remove(key);
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        try
        {
            if (HashMapData.ContainsKey(key))
            {
                value = HashMapData[key];
                return true;
            }
            else
            {
                value = default(TValue);
                return false;
            }
        }
        catch
        {
            value = default(TValue);
            return false;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return HashMapData.GetEnumerator();
    }
}

结尾

将此类放在自己项目的Collections命名空间下,即可在C#中使用HashMap了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0564丶Kang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值