首页 > 其他 > 详细

Contains,Exists,Any,Count 比较是否存在某个元素

时间:2019-02-16 19:49:02      阅读:185      评论:0      收藏:0      [点我收藏+]

 

 

private static void Main(string[] args)
        {
            int count = 100000000;
            Console.WriteLine("判断是否存在某个元素 :");
            Console.WriteLine("\t值类型比较 :");
            Contains_Exists_Any_Test(count);
            Console.WriteLine();
            Console.WriteLine("\t引用类型比较 :");
            Contains_Exists_Any_Test_Complex(count);

            Console.ReadKey();
        }


        /// <summary>
        /// 值类型比较
        /// </summary>
        /// <param name="num"></param>
        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.Reset();
            sw.Start();
            Console.WriteLine(list.Exists(i => i == N));
            sw.Stop();
            Console.WriteLine("Exists:" + sw.Elapsed.ToString());

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


            sw.Reset();
            sw.Start();
            Console.WriteLine(list.Count(i => i == N) > 0);
            sw.Stop();
            Console.WriteLine("Count:" + sw.Elapsed.ToString());
        }


        /// <summary>
        /// 引用类型比较
        /// </summary>
        /// <param name="num"></param>
        public static void Contains_Exists_Any_Test_Complex(int num)
        {
            List<Person> list = new List<Person>();
            Person person = new Person { Id = num };
            int N = num;
            for (int i = 0; i < N; i++)
            {
                list.Add(new Person { Id = i });
            }
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            Console.WriteLine(list.Contains(person));
            sw.Stop();
            Console.WriteLine("Contains:" + sw.Elapsed.ToString());

            sw.Reset();
            sw.Start();
            Console.WriteLine(list.Exists(i => i.Id == person.Id));
            sw.Stop();
            Console.WriteLine("Exists:" + sw.Elapsed.ToString());

            sw.Reset();
            sw.Start();
            Console.WriteLine(list.Any(i => i.Id == person.Id));
            sw.Stop();
            Console.WriteLine("Any:" + sw.Elapsed.ToString());


            sw.Reset();
            sw.Start();
            Console.WriteLine(list.Count(i => i.Id == person.Id) > 0);
            sw.Stop();
            Console.WriteLine("Count:" + sw.Elapsed.ToString());
        }

 

    public class Person : IEquatable<Person>
    {
        public int Id { get; set; }
        public string Name { get; set; }
     public int Age { get; set; }
 
        public override bool Equals(object obj)
        {
            //if (ReferenceEquals(null, obj))
            //{
            //    return false;
            //}

            //if (ReferenceEquals(this, obj))
            //{
            //    return true;
            //}

            //if (obj.GetType() != GetType())
            //{
            //    return false;
            //}

            return Equals((Person)obj);
        }

        public bool Equals(Person other)
        {
            //if (ReferenceEquals(null, other))
            //{
            //    return false;
            //}

            //if (ReferenceEquals(this, other))
            //{
            //    return true;
            //}

            return Id == other.Id;
        }

        public override int GetHashCode()
        {
            return Id;
        }
    }

  

技术分享图片

 

 

结论:

值类型 : Contais > Exists > Any (Count)

引用类型 : Exists > Contains > Any (Count)

 

将对象需要比较的属性增加为3个:

 

list.Exists(i => i.Id == person.Id && i.Age == person.Age && i.Name == person.Name)

 

        public bool Equals(Person other)
        {
            //if (ReferenceEquals(null, other))
            //{
            //    return false;
            //}

            //if (ReferenceEquals(this, other))
            //{
            //    return true;
            //}

            return Id == other.Id && Age == other.Age && Name == other.Name;
        }

 

结果:

技术分享图片

 

结论跟上述一样.

 

Contains,Exists,Any,Count 比较是否存在某个元素

原文:https://www.cnblogs.com/refuge/p/10388671.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!