专题 |  新闻 |  转载文档 |  控件 |  产品 |  人才 |  项目管理 |  博客  
 论坛 |  Windows |  Visual Studio |  ASP.NET |  VB.NET |  C#.NET |  开源项目 |  搜索  
当前所在位置:首页c.net  >  正文
专题: 软件 | 服务器 | IT 电子产品 | PC 电脑 | 笔记本 | IE7 | Windows Mobile | Windows 2003 Server | ISA 2006 | Windows Vista
C#中Hashtable Dictionary的使用方法探索
作者:C#.net 发布时间:20080506 文章来源:

C#中Hashtable Dictionary的使用方法探索
2008-5-5
   要了解C#中Hashtable Dictionary的使用,我们先来看一个例子!

    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 中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便捷。每一存储桶都与一个哈希代码关联,该哈希代码是使用哈希函数生成的并基于该元素的键。 [Page]
    哈希函数是基于键返回数值哈希代码的算法。键是正被存储的对象的某一属性的值。哈希函数必须始终为相同的键返回相同的哈希代码。一个哈希函数能够为两个不同的键生成相同的哈希代码,但从哈希表检索元素时,为每一唯一键生成唯一哈希代码的哈希函数将令性能更佳。
    在 Hashtable 中用作元素的每一对象必须能够使用 GetHashCode 方法的实现为其自身生成哈希代码。但是,还可以通过使用接受 IHashCodeProvider 实现作为参数之一的 Hashtable 构造函数,为 Hashtable 中的所有元素指定一个哈希函数。
    在将一个对象添加到 Hashtable 时,它被存储在存储桶中,该存储桶与匹配该对象的哈希代码的哈希代码关联。在 Hashtable 内搜索一个值时,将为该值生成哈希代码,并且搜索与该哈希代码关联的存储桶。
    例如,一个字符串的哈希函数可以采用该字符串中每一字符的 ASCII 代码并它们添加到一起来生成一个哈希代码。字符串“picnic”将具有与字符串“basket”的哈希代码不同的哈希代码;因此,字符串“picnic”和“basket”将处于不同的存储桶中。与之相比,“stressed”和“desserts”将具有相同的哈希代码并将处于相同的存储桶中。
    Dictionary 类与 Hashtable 类的功能相同。对于值类型,特定类型(不包括 Object)的 Dictionary 的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作,中国自学编程网,www.zxbc.cn
    ----------------------------------------------------------------------------------------------------
    产生这个结果的原因就是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); [Page]
         }

        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());        //不排序 [Page]
             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;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    namespace NoSortHashtable
    {
     /// <summary>
     /// Summary description for Class1.
     /// </summary>
     public class Class1
     {
         /// <summary>
         /// The main entry point for the application.
         /// </summary>
         [STAThread]
         static void Main(string[] args)
         {
             Hashtable ht = new Hashtable();
             ht.Add(\"hunan\",\"changsha\");
             ht.Add(\"beijing\",\"beijing\");
             ht.Add(\"anhui\",\"hefei\");
             ht.Add(\"sichuan\",\"chengdu\");
             foreach(string str in ht.Keys)
             {
                 Console.WriteLine(str + \" : \" + ht[str]);
             }
             Console.WriteLine(\"------------------------------------\");

             Dictionary<String,String> dic = new Dictionary<String,String>();
             dic.Add(\"hunan\",\"changsha\"); [Page]
             dic.Add(\"beijing\",\"beijing\");
             dic.Add(\"anhui\",\"hefei\");
             dic.Add(\"sichuan\",\"chengdu\");
             foreach(string str in dic.Keys)
             {
                 Console.WriteLine(str + \" : \" + dic[str]);
             }

             Console.WriteLine(\"------------------------------------\");

             ListDictionary lsdic = new ListDictionary();
             lsdic.Add(\"hunan\",\"changsha\");
             lsdic.Add(\"beijing\",\"beijing\");
             lsdic.Add(\"anhui\",\"hefei\");
             lsdic.Add(\"sichuan\",\"chengdu\");
            
             foreach(string str in lsdic.Keys)
             {
                 Console.WriteLine(str + \" : \" + lsdic[str]);
                 }
            }
       }
    }
 

转载文档
.NET
Visual Basic
Visual C++
游戏开发
Java
ASP
Perl
Delphi
Javascript
其他开发语言
数据库开发
网站制作技术
文件格式
其他
文章投稿
Web.Service
ASP.Net
C#.Net
VB/VC.Net
Borland
JAVA
数据库
Unix/Linux
图形美工
安全
VS.Net

中国DotNet俱乐部 || 博客.中国  版权所有
京ICP备05037360号