Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

两个集合对比

桂素伟 2019-10-08 18:19:00 阅读数:8 评论数:0 点赞数:0 收藏数:0

在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们。

项目名称:Asp.NetCoreExperiment

项目地址:https://github.com/axzxs2001/Asp.NetCoreExperiment

 

今天先分享几个对集合知识点。

两个集合对比代码:

GitHub代码库(https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Common/CompareCollections

         static void Main(string[] args)
         {
             var leftKes = new List<string>() { "111111", "222222", "333333", "444444" };
             var rightKes = new List<string>() { "333333", "444444", "555555", "666666" };
 
             Console.WriteLine("Left集合");
             Console.WriteLine(string.Join(',', leftKes));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Right集合");
             Console.WriteLine(string.Join(',', rightKes));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Left多的 ");
             Console.WriteLine(string.Join(',', leftKes.Except(rightKes)));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Right多的 ");
             Console.WriteLine(string.Join(',', rightKes.Except(leftKes).ToList()));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Left和Right交集 ");
             Console.WriteLine(string.Join(',', rightKes.Intersect(leftKes)));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Left和Right并集 ");
             Console.WriteLine(string.Join(',', leftKes.Union(rightKes)));
             Console.WriteLine("-------------------------------------");
 
             Console.WriteLine("Left和Right对称差集 ");
             Console.WriteLine(string.Join(',', leftKes.Union(rightKes).Except(rightKes.Intersect(leftKes))));
             Console.WriteLine("-------------------------------------");
 
         }

 

运行速度测试代码:

GitHub代码库(https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Common/CSharp%E8%8E%B7%E5%8F%96%E4%B8%A4%E4%B8%AA%E9%9B%86%E5%90%88%E4%B8%AD%E7%9B%B8%E5%90%8C%E7%9A%84%E5%92%8C%E4%B8%8D%E5%90%8C%E7%9A%84%E7%BB%93%E6%9E%9C

         static void Test()
         {
             Console.WriteLine("-------------------开始初始化集合-------------------");
             #region 制造数据
             var watch = new Stopwatch();
             watch.Start();
             var listA = new List<string>();
             var listB = new List<string>();
             for (int i = ; i < 1000000; i++)
             {
                 var id = Guid.NewGuid().ToString();
                 listA.Add(id);
             }
             listB.AddRange(listA);
             //制造B的差异数据
             for (int i = ; i < ; i++)
             {
                 var tick = DateTime.Now.Ticks;
                 var random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> ));
                 var index = random.Next(, 100000);
                 listB[index] = Guid.NewGuid().ToString();
             }
             watch.Stop();
             TimeSpan span = watch.Elapsed;
             Console.WriteLine("制造数据总共花费{0}ms.", span.TotalMilliseconds);
             #endregion
 
             #region 比较
             Console.WriteLine("-------------------开始比较-------------------");
             Console.WriteLine("-------------------listA中交集外---------------------");
             watch = new Stopwatch();
             watch.Start();
             var listD = listA.Except(listA.Intersect(listB));
             watch.Stop();
             span = watch.Elapsed;
             Console.WriteLine("listA中交集外 总共花费{0}ms.", span.TotalMilliseconds);
             foreach (var item in listD)
             {
                 Console.WriteLine(item);
             }
             Console.WriteLine("----------------------listB中交集外--------------------");
             watch = new Stopwatch();
             watch.Start();
             var listE = listB.Except(listA.Intersect(listB));
             watch.Stop();
             span = watch.Elapsed;
             Console.WriteLine("listB中交集外 总共花费{0}ms.", span.TotalMilliseconds);
             foreach (var item in listE)
             {
                 Console.WriteLine(item);
             }
             Console.WriteLine("--------------listA和listB中交集外-------------------");
             watch = new Stopwatch();
             watch.Start();
             var listF = listA.Concat(listB).Except(listA.Intersect(listB));
             watch.Stop();
             span = watch.Elapsed;
             Console.WriteLine("listA和listB中交集外 总共花费{0}ms.", span.TotalMilliseconds);
             foreach (var item in listF)
             {
                 Console.WriteLine(item);
             }
             #endregion
         }

对比的运行时长结果:

我的配置是:

 

版权声明
本文为[桂素伟]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/axzxs2001/p/11637113.html