博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Hashtable 让哈希表顺序输出。

Posted on 2007-10-24 16:49  liufu627  阅读(1560)  评论(1编辑  收藏  举报

class QueueHashtable<TKey, TValue>
    {
        List<TKey> _keys = new List<TKey>();
        Hashtable ht = new Hashtable();

        public ICollection Keys
        {
            get
            {
                return _keys;
            }
        }
        public ICollection Values
        {
            get
            {
                return ht.Values;
            }
        }      

        public void Clear()
        {
            _keys.Clear();
            ht.Clear();
        }
        /// <summary>
        /// 根据键名得到键值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual TValue this[TKey key]
        {
            get
            {
                return (TValue) ht[key];
            }
            set
            {
                ht[key] = value;
            }

        }
        public void Add(TKey key, TValue value)
        {
            ht.Add(key, value);
            _keys.Add(key);
        }

        public virtual bool ContainsKey(TKey key)
        {
            return ht.ContainsKey(key);
        }

        public virtual bool ContainsValue(TValue value)
        {
            return ht.ContainsValue(value);
        }      
        public virtual void Remove(TKey key)
        {
            ht.Remove(key);
        }
    }

//执行

 const int C_Count = 1000000;
            DateTime start = DateTime.Now;
            TimeSpan ts;

            //begin
            Console.WriteLine();

            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            //add 10000

            start = DateTime.Now;
            for (int i = 0; i < C_Count; i++)
            {
                ht.Add(i, i);
            }
            ts = DateTime.Now - start;
            Console.WriteLine("  System Hashtable add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);


           

            QueueHashtable<int, int> qu = new QueueHashtable<int, int>();
            start = DateTime.Now;
            for (int i = 0; i < C_Count; i++)
            {
                qu.Add(i, i);
            }
            ts = DateTime.Now - start;
            Console.WriteLine(" HashtableQueue<int, int>   add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            start = DateTime.Now;
            foreach (int key in ht.Keys)
                ;
            ts = DateTime.Now - start;
            Console.WriteLine("  int key System Hashtable foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

 

 

            start = DateTime.Now;
            foreach (int key in t.Keys)
                ;
            ts = DateTime.Now - start;
            Console.WriteLine("  HashtableQueue<int, int>  foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            Console.WriteLine();


            //comment
            //Hashtable t = new Hashtable();

            //start = DateTime.Now;
            //for (int i = 0; i < C_Count; i++)
            //{
            //    t.Add(i, i);
            //}
            //ts = DateTime.Now - start;
            //Console.WriteLine(" My Hashtable  add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            //start = DateTime.Now;
            //foreach (int key in t.Keys)
            //    ;
            //ts = DateTime.Now - start;
            //Console.WriteLine("  My Hashtable foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

        }