zoukankan      html  css  js  c++  java
  • hash tabel 与 dictionary 的区别

    1 :

    Hashtable 支持多个线程读, 同时只有一个线程写 , dictionary 不支持

    Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary<TKey, TValue>

    2 : hash table 只能用 key 引用, 而不能 foreach , dictionary 可以。

    引用 hash table:

    public void MethodHashTable()
    {
        Hashtable objHashTable = new Hashtable();
        objHashTable.Add(1, 100);    // int
        objHashTable.Add(2.99, 200); // float
        objHashTable.Add('A', 300);  // char
        objHashTable.Add("4", 400);  // string
    
        lblDisplay1.Text = objHashTable[1].ToString();
        lblDisplay2.Text = objHashTable[2.99].ToString();
        lblDisplay3.Text = objHashTable['A'].ToString();
        lblDisplay4.Text = objHashTable["4"].ToString();
    
    
        // ----------- Not Possible for HashTable ----------
        //foreach (KeyValuePair<string, int> pair in objHashTable)
        //{
        //    lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
        //}
    }

    引用 dictionary :

      public void MethodDictionary()
      {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        dictionary.Add("cat", 2);
        dictionary.Add("dog", 1);
        dictionary.Add("llama", 0);
        dictionary.Add("iguana", -1);
    
        //dictionary.Add(1, -2); // Compilation Error
    
        foreach (KeyValuePair<string, int> pair in dictionary)
        {
            lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
        }
      }
  • 相关阅读:
    QT设置32bit 64bit编译
    windows应用程序无边框设置
    分组与聚合数据
    快速排序
    【leetcode】two sum --medium
    【leetcode】path sum--easy
    【leetcode】happy number--easy
    SQL函数
    数据库的高级设计
    c++笔试准备(二)数组全排的问题
  • 原文地址:https://www.cnblogs.com/lthxk-yl/p/3553775.html
Copyright © 2011-2022 走看看