Unity3d+c#:Dictionary多个key查找一个value,重载Equals

时间:2022-11-01 18:01:17


Equals与GetHashCode

System.Object声明方法Equals和GetHashCode以及其他成员。 (注意:这个案例在C#中很重要)。您创建的类型会自动继承这些方法。

Equals的任务是将一个对象与另一个对象进行比较。引用类型的默认实现是比较引用。如果你想改变这种行为,你将不得不重写这个方法。

GetHashCode计算对象的哈希码并用于哈希表。例如,类型Dictionary<TKey,TValue>和HashSet利用它。 请参阅Hashtable and Dictionary Collection Types。如果您覆盖Equals,则必须覆盖GetHashCode以保持一致性。

DataKey1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DictionaryEx
{
public class DataKey1
{
public int mKey1;

public DataKey1(int key)
{
mKey1 = key;
}

public override bool Equals(object obj)
{
if (obj == null) return false;
DataKey1 key = obj as DataKey1;
if (key == null) return false;

return this.mKey1 == key.mKey1;
}

public override int GetHashCode()
{
return mKey1.GetHashCode();
}
}

public class DataKey2 : DataKey1
{
public int mKey2;

public DataKey2(int key1, int key2)
: base(key1)
{
mKey2 = key2;
}

public override bool Equals(object obj)
{
if (obj == null) return false;
DataKey2 key = obj as DataKey2;
if (key == null) return false;

return this.mKey1 == key.mKey1 && this.mKey2 == key.mKey2;
}

public override int GetHashCode()
{

return base.GetHashCode() ^ mKey2.GetHashCode();
}
}

public class DataKey3 : DataKey2
{
public int mKey3;

public DataKey3(int key1, int key2, int key3)
: base(key1, key2)
{
mKey3 = key3;
}

public override bool Equals(object obj)
{
if (obj == null) return false;
DataKey3 key = obj as DataKey3;
if (key == null) return false;

return this.mKey3 == key.mKey3 && this.mKey2 == key.mKey2 && this.mKey1 == key.mKey1;
}

public override int GetHashCode()
{
return base.GetHashCode() ^ mKey3.GetHashCode();
}
}

public class DataKey4 : DataKey3
{
public int mKey4;

public DataKey4(int key1, int key2, int key3, int key4)
: base(key1, key2, key3)
{
mKey4 = key4;
}

public override bool Equals(object obj)
{
if (obj == null) return false;
DataKey4 key = obj as DataKey4;
if (key == null) return false;

return this.mKey4 == key.mKey4 && this.mKey3 == key.mKey3 && this.mKey2 == key.mKey2 && this.mKey1 == key.mKey1;
}

public override int GetHashCode()
{
return base.GetHashCode() ^ mKey4.GetHashCode();
}
}
}

重载 public override bool Equals(object obj)
使类的比较为比较里面的成员变量int值

TableData

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DictionaryEx
{
public class TableData<K, T> //: CountString
where K : DataKey1
{
protected const int mcHeadSize = 140;
protected string mTableName;
protected string mChineseTableName = "";
protected int mTimeStamp;
protected int mCurSeq;
protected Dictionary<K, T> mTableData = new Dictionary<K, T>(131);

protected List<T> mTableDataSequence = new List<T>(128);

public TableData(string tableName, string strChineseTableName)
{
mTableName = tableName;
mChineseTableName = strChineseTableName;//上线后屏蔽
}

public TableData()
{

}

public string name
{
get
{
return mTableName;
}
}

public int timeStamp
{
set
{
mTimeStamp = value;
}
get
{
return mTimeStamp;
}
}

public int curSeq
{
set
{
mCurSeq = value;
}
get
{
return mCurSeq;
}
}

//
public List<T> GetTableSequenceData()
{
return mTableDataSequence;
}

public Dictionary<K, T> GetTableKeyValueData()
{
return mTableData;
}

public Int32 Count()
{
return mTableData.Count;
}

public T Find(K key)
{
T ret;

if (mTableData.TryGetValue(key, out ret)) return ret;
return default(T); //此关键字对于引用类型会返回空,对于数值类型会返回零。对于结构,此关键字将返回初始化为零或空的每个结构成员
}

public void Add(K key,T data)
{
mTableDataSequence.Add(data);
mTableData.Add(key, data);
}

public void Remove(K key)
{
T data;
if (mTableData.TryGetValue(key, out data))
{
mTableDataSequence.Remove(data);
mTableData.Remove(key);
}
}
}
}

使用与测试

TableData<DataKey2, int> dic = new TableData<DataKey2, int>();
dic.Add(new DataKey2(1, 2), 1);
dic.Add(new DataKey2(3,4), 2);

Debug.Log(dic.Find(new DataKey2(1, 2)));

源码

​https://github.com/luoyikun/UnityForTest​​ DictionaryExScene场景