背景我的同(
范
)事(磊
),这个逼以前是干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了