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

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场景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四夕立羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值