C# hashset,HashTable

本文对比了Java中HashMap与Hashtable的几个关键不同之处,包括同步性、对null的支持、迭代顺序预测以及性能等方面,并提供了.NET中Dictionary和HashSet的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.NET 没有hashmap,hashmap是Java容器;

There are several differences between HashMap and Hashtable in Java:

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

Since synchronization is not an issue for you, I'd recommend HashMap. If synchronization becomes an issue, you may also look at ConcurrentHashMap

https://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable

//dictionary
        Dictionary<byte, string> mdic = new Dictionary<byte, string>();
        mdic.Add(1, "Yaodada");
        mdic.Add(2, "Meiei");
        mdic.Add(3, "DFdsff");


        foreach(KeyValuePair<byte,string> kvp in mdic)
        {
            Console.Write("key={0},value={1}", kvp.Key, kvp.Value);
        }
        Console.WriteLine("\n");
        Console.Write("consoleWrite:"+mdic[1]+"\n");
        string name;
        if (mdic.TryGetValue(2, out name)) {
            Console.Write(name+"\n");
        }else
        {
            Console.Write("key not found..."+"\n");
        }


   //hashset
        string[] array = new string[] { "AA", "BB", "CC", "DD", "CC", "BB" };
        HashSet<string> mhash1 = new HashSet<string>(array);
        Console.Write("mhash1: "+mhash1.Count+"\n");
        foreach(string s in mhash1) {
            Console.Write(s);
        }
        Console.Write("------------------------------------"+"\n");
        string[] array2 = new string[] { "QQ", "WW", "EE", "FF", "GG" };
        HashSet<string> mhash2 = new HashSet<string>(array2);
        Console.Write("mhash2: " + mhash2.Count + "\n");
        foreach (string s in mhash2)
        {
            Console.Write(s);
        }
        Console.Write("------------------------------------" + "\n");
        mhash1.UnionWith(mhash2);
        Console.Write("new mhash1: " + mhash1.Count + "\n");
        foreach (string s in mhash1)
        {
            Console.Write(s+"\n");
        }
   //hashtable
        Hashtable mtable = new Hashtable();
        mtable.Add(1, "I");
        mtable.Add(2, "See");
        mtable.Add(3, "you");
        foreach(DictionaryEntry d in mtable)
        {
            Console.Write("key={0},value={1};\n", d.Key, d.Value);
        }
        Console.ReadKey();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值