惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
IT之家
IT之家
T
Tailwind CSS Blog
月光博客
月光博客
Vercel News
Vercel News
V
V2EX
Engineering at Meta
Engineering at Meta
B
Blog
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
腾讯CDC
I
InfoQ

博客园 - Mat

VBScript Functions and Keyword - Mat Windows Management Interface (WMI) 用div布局时怎么让文字或图片垂直居中对齐呢? CSS控制的内容超过容器宽度后显示省略号 div内容可编辑 Access C#从视频截图的方法 异步工作流体系结构的选择 笔记 vs2005中的WebBrowser控件的简单应用 CSS 布局 如何在 ASP.NET 应用程序中实现模拟 再谈css--如何针对不同位置的元素使用不同的风格 给web用户控件自定义后台事件 《冰鉴》—— 《金刚经》 《道子》——大智慧 排序算法 .Net C# 集合
Hashtable Dictionary
Mat · 2006-10-20 · via 博客园 - Mat

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

----------------------------------------------------------------------------------------------------

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
    }

}


另外,System.Collections.Specialized.ListDictionary也是可以完成该要求的,不过。。。

使用单链接列表实现 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");
            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]);
            }
       }
  }
 }