【问题标题】:Sorting Hashtable by Order in Which It Was Created按创建顺序对哈希表进行排序
【发布时间】:2011-05-04 02:23:30
【问题描述】:

这类似于How to keep the order of elements in hashtable,除了.NET。

.NET 中是否有任何HashtableDictionary 允许您按照添加到集合中的顺序访问它的.Index 属性?

【问题讨论】:

    标签: c# .net vb.net collections hashtable


    【解决方案1】:

    NameValueCollection 可以通过索引检索元素(但您不能请求特定键或元素的索引)。所以,

    var coll = new NameValueCollection();
    coll.Add("Z", "1");
    coll.Add("A", "2");
    Console.WriteLine("{0} = {1}", coll.GetKey(0), coll[0]); // prints "Z = 1"
    

    但是,当您多次添加键时,它的行为很奇怪(与 IDictionary 相比):

    var coll = new NameValueCollection();
    coll.Add("Z", "1");
    coll.Add("A", "2");
    coll.Add("Z", "3");
    Console.WriteLine(coll[0]); // prints "1,3"
    

    然而,这种行为有据可查。

    注意:NameValueCollection 确实实现IDictionary


    顺便说一句:Dictionary<K,V> 没有任何索引可以使用,但只要你只添加元素,从不删除任何元素,元素的顺序就是插入顺序。请注意,这是 Microsoft 当前实现的细节:文档明确指出顺序是随机的,因此这种行为可能会在 .NET Framework 或 Mono 的未来版本中发生变化。

    【讨论】:

    • 这很棒。简单的 Hashtable 和 Dictionary 的替代方案。另外,关于 Dictionary 的默认排序顺序的注释也很有帮助。
    • 记得添加:using System.Collections.Specialized;
    【解决方案2】:

    如果这是您需要有效跟踪的内容,那么您使用了错误的数据结构。相反,您应该使用SortedDictionary,其中键标有添加时间的索引(或时间戳)和自定义IComparer,它根据索引(或时间戳)比较两个键。

    【讨论】:

      【解决方案3】:

      .NET 中是否有任何 Hashtable 或 Dictionary 允许您按照条目添加到集合中的顺序访问它的 .Index 属性?

      没有。您可以枚举 Hastable 或 Dictionary 中的所有项目,但这些项目并不一定是按任何顺序排列的(很可能不是)

      您必须要么完全使用不同的数据结构(例如 SortedDictionary 或 SortedList),要么使用单独的列表来存储它们的添加顺序。您可能希望将有序列表和您的字典/哈希表包装在另一个类中以使它们保持同步。

      【讨论】:

        【解决方案4】:

        您可以使用单独的列表来按添加顺序存储元素。类似于以下示例的内容:

        public class ListedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
        {
            List<TValue> _list = new List<TValue>();
            Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey,TValue>();
        
            public IEnumerable<TValue> ListedValues
            {
                get { return _list; }
            }
        
            public void Add(TKey key, TValue value)
            {
                _dictionary.Add(key, value);
                _list.Add(value);
            }
        
            public bool ContainsKey(TKey key)
            {
                return _dictionary.ContainsKey(key);
            }
        
            public ICollection<TKey> Keys { get { return _dictionary.Keys; } }
        
            public bool Remove(TKey key)
            {
                _list.Remove(_dictionary[key]);
                return _dictionary.Remove(key);
            }
        
            // further interface methods...
        }
        

        【讨论】:

          【解决方案5】:

          看看 OrderedDictionary 类。您不仅可以通过键访问它,还可以通过索引(位置)访问它。

          【讨论】:

            【解决方案6】:

            另一种方法是创建一个结构数组,而不是使用

            dictionary.Add{"key1","value1"}
            

            您使用以下键/值创建结构:

            public struct  myStruct{
                private string _sKey;
                public string sKey{
                    get { return _sKey; }
                    set { _sKey = value; }
                }
                private string _sValue;
                public string sValue {
                    get { return _sValue; }
                    set { _sValue = value; }
                }
            }
            
            // create list here
            List<myStruct> myList = new List<myStruct>();
            
            // create an instance of the structure to add to the list
            myStruct item = new myStruct();
            item.sKey = "key1";
            item.sValue = "value1";
            
            // then add the structure to the list
            myList.Add(item);
            

            使用这种方法,您可以毫不费力地向列表中添加额外的维度,只需在结构中添加一个新成员即可。

            注意,如果您需要在添加后修改列表中的项目,则必须将结构更改为类。有关此问题的更多信息,请参阅此页面:error changing value of structure in a list

            【讨论】:

              猜你喜欢
              • 2011-12-09
              • 2013-06-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多