上代码,主要是学习到了如何对比出两个List集合的和差交并集方法
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("-------------------------------------"); }
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 = 0; i < 1000000; i++) { var id = Guid.NewGuid().ToString(); listA.Add(id); } listB.AddRange(listA); //制造B的差异数据 for (int i = 0; i < 10; i++) { var tick = DateTime.Now.Ticks; var random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); var index = random.Next(1, 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
标签:Console,watch,var,WriteLine,集合,两个,listA,对比,listB From: https://www.cnblogs.com/djd66/p/17121936.html