List中對比Contains, Exists, Any之間的優缺點

風靈使發表於2018-06-05

List<T>中,Contains, Exists, Any都可以實現判斷元素是否存在。

先上結果。效能方面:Contains 優於 Exists 優於 Any

以下為測試程式碼

public static void Contains_Exists_Any_Test(int num)
 {
            List<int> list = new List<int>();

            int N = num;
            for (int i = 0; i < N; i++)
            {
                list.Add(i);
            }
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            Console.WriteLine(list.Contains(N));
            sw.Stop();
            Console.WriteLine("Contains:"+sw.Elapsed.ToString());

            sw.Restart();
            Console.WriteLine(list.Exists(i => i == N));
            sw.Stop();
            Console.WriteLine("Exists:"+ sw.Elapsed.ToString());

            sw.Restart();
            Console.WriteLine(list.Any(i => i == N));
            sw.Stop();
            Console.WriteLine("Any:"+ sw.Elapsed.ToString());
}

在開發過程中可以根據實際情況進行選擇,當list中資料量不大時使用Exists程式碼更簡潔易懂;資料量大時推薦使用Contains;不推薦使用Any

下面的程式碼對比就能看出為啥資料量不大的時候推薦Exists了。

class ITEM_GIDComparer : IEqualityComparer<T>
    {
        public bool Equals(T orl1, T orl2)
         {
             if (orl1==null)
             {
                 return orl2 == null;
             }
             return orl1.ITEM_GID == orl2.ITEM_GID;
         }

        public int GetHashCode(T orl)
         {
             if (orl == null)
                 return 0;
             return orl.ITEM_GID.GetHashCode();
         } 
    }
    orlclst.Contains(orlc, new ITEM_GIDComparer())
    //Exists一行程式碼就可以實現上面的功能
    orlclst.Exists(x=>x.ITEM_GID==orlc.ITEM_GID)


C#比較兩個list集合,兩集合同時存在或A集合存在B集合中無

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Test
{
    class Program 
    {
        static void Main(string[] args)
        {
            var student1 = new List<student>();
            student1.Add(new student() { name = "張三", subject = "英語", score = 89 });
            student1.Add(new student() { name = "李四", subject = "英語", score = 95 });
            student1.Add(new student() { name = "王五", subject = "英語", score = 69 });
            student1.Add(new student() { name = "李倩", subject = "英語", score = 99 });

            var student2 = new List<student>();
            student2.Add(new student() { name = "李四", subject = "英語", score = 95 });
            student2.Add(new student() { name = "王五", subject = "數學", score = 69 });
            student2.Add(new student() { name = "趙六", subject = "數學", score = 100 });

            //var exp1 = student1.Where(a => student2.Any(t => a.name.Contains(t.name))).ToList();  
            //使用Exists同樣可以實現 字面上應該更好理解,而且效率要高些  
            var exp1 = student1.Where(a => student2.Exists(t => a.name.Contains(t.name))).ToList();
            Console.WriteLine("--查詢student1和student2總同時存在的資料--");
            foreach (var item in exp1)
            {
                Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
            }

            //var exp2 = student1.Where(a => student2.All(t => !a.name.Contains(t.name))).ToList();    
            //使用Exists同樣可以實現 字面上應該更好理解,而且效率要高些  
            var exp2 = student1.Where(a => !student2.Exists(t => a.name.Contains(t.name))).ToList();
            Console.WriteLine("--查詢student1集合中存在,而student2不存在的資料--");
            foreach (var item in exp2)
            {
                Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
            }

            var exp3 = student2.Where(a => !student1.Exists(t => a.name.Contains(t.name))).ToList();
            Console.WriteLine("--查詢student2集合中存在,而student1不存在的資料--");
            foreach (var item in exp3)
            {
                Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
            }
            Console.Read();   
             
        }

        public class student
        {
            /// <summary>    
            /// 姓名    
            /// </summary>    
            public string name;
            /// <summary>    
            /// 科目    
            /// </summary>    
            public string subject;
            /// <summary>    
            /// 分數    
            /// </summary>    
            public int score;
        }

    }
        
}

執行結果如圖:

這裡寫圖片描述

相關文章