• List集合删除方法


     class Program
        {
            private static Random random = new Random((int)DateTime.Now.Ticks);
            static void Main(string[] args)
            {
    
                Console.WriteLine("Be patient, generating data...");
    
                List<string> list = new List<string>();
                List<string> toRemove = new List<string>();
                for (int x = 0; x < 1000000; x++)
                {
                    string randString = RandomString(random.Next(100));
                    list.Add(randString);
                    if (random.Next(1000) == 0)
                        toRemove.Insert(0, randString);
                }
    
                List<string> l1 = new List<string>(list);
                List<string> l2 = new List<string>(list);
                List<string> l3 = new List<string>(list);
                List<string> l4 = new List<string>(list);
                List<string> l5 = new List<string>(list);
    
                Console.WriteLine("Be patient, testing...");
    
                Stopwatch sw1 = Stopwatch.StartNew();
                l1.RemoveAll(toRemove.Contains);
                sw1.Stop();
    
                Stopwatch sw2 = Stopwatch.StartNew();
                l2.RemoveAll(new HashSet<string>(toRemove).Contains);
                sw2.Stop();
    
                Stopwatch sw3 = Stopwatch.StartNew();
                l3 = l3.Except(toRemove).ToList();
                sw3.Stop();
    
                Stopwatch sw4 = Stopwatch.StartNew();
                l4 = l4.Except(new HashSet<string>(toRemove)).ToList();
                sw4.Stop();
    
    
    
                //Stopwatch sw5 = Stopwatch.StartNew();
                //l5 = l5.RemoveAll((x)=>x[0]==);
                //sw5.Stop();
    
    
                Console.WriteLine("L1.Len = {0}, Time taken: {1}ms", l1.Count, sw1.Elapsed.TotalMilliseconds);
                Console.WriteLine("L2.Len = {0}, Time taken: {1}ms", l1.Count, sw2.Elapsed.TotalMilliseconds);
                Console.WriteLine("L3.Len = {0}, Time taken: {1}ms", l1.Count, sw3.Elapsed.TotalMilliseconds);
                Console.WriteLine("L4.Len = {0}, Time taken: {1}ms", l1.Count, sw4.Elapsed.TotalMilliseconds);
    
                Console.ReadKey();
                //
                //authorsList.RemoveAll((x) => x.firstname == "Bob");
    
                //authorsList = authorsList.Except(authors).ToList();
    
            }
            private static string RandomString(int size)
            {
                StringBuilder builder = new StringBuilder();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
    
                return builder.ToString();
            }
        }
    

      

     备注:这是网上找的例子大致方案就是这几种,具体使用哪种看自己的实际需求

  • 相关阅读:
    多轨车皮编序问题
    [Luogu1032] 字串变换
    [POJ1101] The Game
    Linux 下源码编译FFMEG
    交叉编译 tcpdump
    现代电视原理-电视传像原理
    Dos:‘锘緻echo’ 不是内部或外部命令,也不是可运行的程序或批处理文件
    Win7 登入壁纸修改
    Office 2016安装后的优化设置
    Linux 系统目录结构
  • 原文地址:https://www.cnblogs.com/myloveblogs/p/7605118.html
Copyright © 2020-2023  润新知