用Dictionary 挺好还支持泛型

解决方案 »

  1.   

    不知道是不是因为Contains要调用string.GetHashCode很多次,执行时间慢。创建索引可以么?
    List<string> a;替换为Dictionary<int, string> a;
    添加的地方替换为
    a.Add(a.GetHashCode(), a);
    搜索的地方替换为
    if(a.ContainsKey(a.GetHashCode())
    {
    ...
    }
    这样可以缓存hashcode避免List的Contains方法要调用每个元素的GetHashCode方法。没实际测试,理论上可行。楼主你试试。
      

  2.   

    主要是减少GetHashCode的调用和执行。
      

  3.   

    实在不行,只有自己写一个查找的算法,但不知道效率有没有.NET自带的效率高
      

  4.   

    SortedList或者SortedDictionary, 折半查找
      

  5.   

    linq 查询
    分多个多线程查询
      

  6.   

    用的是.NET 2.0  不好用LINQ查询吧
      

  7.   

    Dictionary<int, string> a = new Dictionary<int, string>();
    Dictionary<int, string> b = new Dictionary<int, string>();
    Dictionary<int, string> c = new Dictionary<int, string>();foreach (int acode in a.Keys)
    {
        if (b.ContainsKey(acode))
        {
        }
        else
        {
            c.Add(acode, a[acode]);
        }
    }
      

  8.   

    3楼的
    if(a.ContainsKey(a.GetHashCode())
    {
    ...
    }
    应该改为
    if(b.ContainsKey(a.GetHashCode())
    {
    ...
    }
    吧?
      

  9.   

    不知道如何添加a就看看3楼回帖:
    string newstring = "aaa";
    a.Add(newstring.GetHashCode(),newstring);
      

  10.   


                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();            List<string> list1 = Enumerable.Range(0, 10000).Select(s => s.ToString()).ToList();
                List<string> list2 = Enumerable.Range(0, 10000).Select(s => s.ToString()).ToList();
                watch.Start();
                Dictionary<int, string> dic1 = list1.ToDictionary(c => c.GetHashCode());
                Dictionary<int, string> dic2 = list2.ToDictionary(c => c.GetHashCode());
                List<string> resut = new List<string>();
                foreach (System.Collections.Generic.KeyValuePair<int, string> item in dic1)
                {
                    if (dic2.ContainsKey(item.Key))
                    {
                        resut.Add(item.Value);
                    }
                }
                watch.Stop();
                System.Console.WriteLine(watch.ElapsedMilliseconds.ToString());
                //结果7
                resut .Clear ();
                watch.Start();
                foreach (string str in list1)
                {
                   if (list2 .Contains (str))
                       resut .Add (str);
                }
                watch.Stop();
                System.Console.WriteLine(watch.ElapsedMilliseconds.ToString());
                //结果 948
      

  11.   

    用Linq写了一个看看!效率一般。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> a = new List<string> { "Edwin","edwin","wed","red","Haha"};
                for (int i = 0; i < 100000; i++)
                {
                    a.Add(System.Guid.NewGuid().ToString().Substring(0,6));
                }
                System.Console.WriteLine(a.Count);
                List<string> b = new List<string> { "Edwin", "edwin", "wed", "red", "Haha" ,"54544","9494","I CALL"};
                for (int i = 0; i < 10000; i++)
                {
                    b.Add(System.Guid.NewGuid().ToString().Substring(0, 6));
                }
                System.Console.WriteLine(b.Count);
                List<string> temp = null;
                Stopwatch sw = new Stopwatch();
                sw.Start();           
                if (a.Count > b.Count)
                {
                    var v = from p in a where b.Contains(p) select p;
                    temp = v.ToList();
                }
                else
                {
                    var v = from p in b where a.Contains(p) select p;
                    temp = v.ToList();
                }
                sw.Stop();
                System.Console.WriteLine("----------------------------------------------");
                System.Console.WriteLine(temp.Count);
                System.Console.WriteLine(sw.Elapsed.Milliseconds / 1000d);
                System.Console.ReadLine();
            }
     
        }
    }100005
    10000
    68
    0.261