先看下面的代码
using
System;
using System.Collections; namespace NoSortHashtable { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main( string [] args) { Hashtable hashTable = new Hashtable(); hashTable.Add( " hunan " , " changsha " ); hashTable.Add( " beijing " , " beijing " ); hashTable.Add( " anhui " , " hefei " ); hashTable.Add( " sichuan " , " chengdu " ); foreach ( string str in hashTable.Keys) { Console.WriteLine(str + " : " + hashTable[str]); } } } } 打印的结果是: anhui : hefei hunan : changsha sichuan : chengdu beijing : beijing 为何产生这样的结果? 我查了MSDN后发现
----------------------------------------------------------------------------------------------------
Hashtable 对象由包含集合元素的存储桶组成。存储桶是 Hashtable 中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便捷。每一存储桶都与一个哈希代码关联,该哈希代码是使用哈希函数生成的并基于该元素的键。 哈希函数是基于键返回数值哈希代码的算法。键是正被存储的对象的某一属性的值。哈希函数必须始终为相同的键返回相同的哈希代码。一个哈希函数能够为两个不同的键生成相同的哈希代码,但从哈希表检索元素时,为每一唯一键生成唯一哈希代码的哈希函数将令性能更佳。 在 Hashtable 中用作元素的每一对象必须能够使用 GetHashCode 方法的实现为其自身生成哈希代码。但是,还可以通过使用接受 IHashCodeProvider 实现作为参数之一的 Hashtable 构造函数,为 Hashtable 中的所有元素指定一个哈希函数。 在将一个对象添加到 Hashtable 时,它被存储在存储桶中,该存储桶与匹配该对象的哈希代码的哈希代码关联。在 Hashtable 内搜索一个值时,将为该值生成哈希代码,并且搜索与该哈希代码关联的存储桶。 例如,一个字符串的哈希函数可以采用该字符串中每一字符的 ASCII 代码并它们添加到一起来生成一个哈希代码。字符串“picnic”将具有与字符串“basket”的哈希代码不同的哈希代码;因此,字符串“picnic”和“basket”将处于不同的存储桶中。与之相比,“stressed”和“desserts”将具有相同的哈希代码并将处于相同的存储桶中。 Dictionary 类与 Hashtable 类的功能相同。对于值类型,特定类型(不包括 Object)的 Dictionary 的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。 ----------------------------------------------------------------------------------------------------
产生这个结果的原因就是Hashtable内部的排序机制使然,但我现在就是不想排序,我按什么顺序输入的,就想它再怎么给我输出,怎么办?
google后发现几个可以解决的办法,不过都需要自己写代码实现
比如,继承hashtable,使用不自动排序的arraylist做中间桥
using
System;
using System.Collections; namespace NoSortHashtable { public class NoSortHashtable : Hashtable { private ArrayList keys = new ArrayList(); public NoSortHashtable() { } public override void Add(object key, object value) { base.Add (key, value); keys.Add (key); } public override ICollection Keys { get { return keys; } } public override void Clear() { base.Clear (); keys.Clear (); } public override void Remove(object key) { base.Remove (key); keys.Remove (key); } public override IDictionaryEnumerator GetEnumerator() { return base.GetEnumerator (); } } }
或者
只要Compare函数的返回结果不等于0就可以添加相同的Key,这样可以实现既可以排序,又可以有相同的Key值,可能在某些情况下会用得到。
using
System;
using System.Collections; namespace testSortedList { class Class1 { [STAThread] static void Main( string [] args) { SortedList sl = new SortedList( new MySort()); // 不排序 sl.Add( 333 , 333 ); sl.Add( 111 , 111 ); sl.Add( 222 , 222 ); sl.Add( 111 , 112 ); PrintList(sl); Console.ReadLine(); } private static void PrintList(SortedList sl) { for ( int i = 0 ;i < sl.Count ;i ++ ) { Console.WriteLine( " {0}/t{1} " ,sl.GetKey(i),sl.GetByIndex(i)); } // end for } // end fn() } public class MySort:IComparer { #region IComparer 成员 public int Compare( object x, object y) { return - 1 ; // 排序 // int iResult = (int)x - (int)y; // if(iResult == 0) iResult = -1; // return iResult; } #endregion } } 使用单链接列表实现 IDictionary。建议用于通常包含 10 个或 10 个以下项的集合。 最后我测试了使用泛类型的Dictionary<T,T>, 尽管msdn上说hashtable和Dictionary的实现是一样的,不过同样的数据,返回的结果却是不同的,我没有找到更多的解释,测试代码如下 using System; namespace NoSortHashtable
另外,System.Collections.Specialized.ListDictionary也是可以完成该要求的,不过。。。
Hashtable 和 Dictionary <K, V> 类型
Hashtable 类和 Dictionary<(Of <(TKey, TValue>)>) 泛型类实现 IDictionary 接口 Dictionary<(Of <(TKey, TValue>)>) 泛型类还实现 IDictionary<(Of <(TKey, TValue>)>) 泛型接口。因此,这些集合中的每个元素都是一个键/值对。 Dictionary<(Of <(TKey, TValue>)>) 类与 Hashtable 类的功能相同 HashTable ht=new HashTable();//实现 IDictionary接口 Dictionary<int,string> myDictionary=new Dictionary<int,string>();//实现IDictionary接口,IDictionary<T key,T value>类 //使用索引器来取值时,如果键不存在就会引发异常 即:double valueTest if(!MyDictionary.TryGetValue(key,out valueTest)) valueTest=0.0; //如果不存在这样的KEY VALUE那么 valueTest=0.0 ,要么就把真实的value赋给valueTest |
Hashtable Dictionary
最新推荐文章于 2024-04-13 16:16:27 发布
.NET(C#) Hashtable Dictionary 探索
2008-11-18 21:36