zoukankan      html  css  js  c++  java
  • Contains,Exists,Any,Count 比较是否存在某个元素

    private static void Main(string[] args)
            {
                int count = 100000000;
                Console.WriteLine("判断是否存在某个元素 :");
                Console.WriteLine("	值类型比较 :");
                Contains_Exists_Any_Test(count);
                Console.WriteLine();
                Console.WriteLine("	引用类型比较 :");
                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;
            }

    结果:

    结论跟上述一样.

  • 相关阅读:
    FZU OJ 1056 :扫雷游戏
    HPU 1166: 阶乘问题(一)
    常用的一些模板
    PAT天梯:L1-019. 谁先倒
    HPU 1437: 王小二的求值问题
    《编程珠玑》阅读小记(7) — 代码调优与节省空间
    《编程珠玑》阅读小记(6) — 算法设计技术
    《编程珠玑》阅读小记(5) — 编程小事
    《编程珠玑》阅读小记(4) — 编写正确的程序
    《C/C++专项练习》— (1)
  • 原文地址:https://www.cnblogs.com/refuge/p/10388671.html
Copyright © 2011-2022 走看看