美文网首页
多Value Dictionary

多Value Dictionary

作者: RichMartin | 来源:发表于2019-12-02 10:34 被阅读0次

使用方式

var m = new MultiDictionary<string, string>();

m.Add( "key", "v1" );
 m.Add( "key", "v2" );
 m.Add( "key", "v3" );
 m.Add( "key2", "v4", "v5" );

 m.Remove( "key2", "v5" );
 m.Remove( "key" );

 m.Clear();

 if ( m.Contains( "key2", "v4" ) )
 {
 }

 if ( m.ContainsKey( "key2" ) )
 {
 }

 foreach ( var pair in m )
 {
     foreach ( var n in pair.Value )
     {
         Debug.Log( pair.Key + ": " + n );
     }
 }

 foreach ( var key in m.Keys )
 {
     Debug.Log( key );
 }

 foreach ( var value in m.Values )
 {
     foreach ( var n in value )
     {
         Debug.Log( n );
     }
 }

 Debug.Log( m.Count );

核心dictionary

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

namespace Martin {
    public class M_DictionaryMutiValue<TKey, TValue> {
        private readonly Dictionary<TKey, List<TValue>> mDictionary = new Dictionary<TKey, List<TValue>> ();

        /// <summary>
        /// 获取或设置与指定键关联的多个值
        /// </summary>
        public List<TValue> this [TKey key] {
            get { return mDictionary[key]; }
            set { mDictionary[key] = value; }
        }

        /// <summary>
        /// 获取包含键的集合
        /// </summary>
        public Dictionary<TKey, List<TValue>>.KeyCollection Keys {
            get { return mDictionary.Keys; }
        }

        /// <summary>
        /// 获取包含多个值的集合
        /// </summary>
        public Dictionary<TKey, List<TValue>>.ValueCollection Values {
            get { return mDictionary.Values; }
        }

        /// <summary>
        /// 获取存储的键/值对的数量
        /// </summary>
        public int Count {
            get { return mDictionary.Count; }
        }

        /// <summary>
        /// 将指定的键和值添加到字典中
        /// </summary>
        public void Add (TKey key, TValue value) {
            if (!mDictionary.ContainsKey (key)) {
                mDictionary.Add (key, new List<TValue> ());
            }
            mDictionary[key].Add (value);
        }

        /// <summary>
        /// 将指定的键和多个值添加到字典中
        /// </summary>
        public void Add (TKey key, params TValue[] values) {
            foreach (var n in values) {
                Add (key, n);
            }
        }

        /// <summary>
        /// 将指定的键和多个值添加到字典中
        /// </summary>
        public void Add (TKey key, IEnumerable<TValue> values) {
            foreach (var n in values) {
                Add (key, n);
            }
        }

        /// <summary>
        /// 使用指定的键删除值
        /// </summary>
        public bool Remove (TKey key, TValue value) {
            return mDictionary[key].Remove (value);
        }

        /// <summary>
        /// 使用指定键删除多个值
        /// </summary>
        public bool Remove (TKey key) {
            return mDictionary.Remove (key);
        }

        /// <summary>
        /// 删除所有键和多个值
        /// </summary>
        public void Clear () {
            mDictionary.Clear ();
        }

        /// <summary>
        /// 确定是否存储指定的键和值
        /// </summary>
        public bool Contains (TKey key, TValue value) {
            return mDictionary[key].Contains (value);
        }

        /// <summary>
        /// 确定是否存储指定的键
        /// </summary>
        public bool ContainsKey (TKey key) {
            return mDictionary.ContainsKey (key);
        }

        /// <summary>
        /// 返回一个枚举器来迭代
        /// </summary>
        public IEnumerator<KeyValuePair<TKey, List<TValue>>> GetEnumerator () {
            foreach (var n in mDictionary) {
                yield return n;
            }
        }
    }
}

相关文章

网友评论

      本文标题:多Value Dictionary

      本文链接:https://www.haomeiwen.com/subject/oifqyctx.html