• 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;
            }

    结果:

    结论跟上述一样.

  • 相关阅读:
    OpenShift和F5的集成手册
    OpenShift负载分区策略(Router Shading)
    Istio在Openshift 3.11的安装
    Openshift 和Harbor的集成
    OpenShift 如何获取bearer Token以便进行各种API调用
    Openshift 3.11和LDAP的集成
    Openshift 节点添加和删除
    Spring Dataflow批处理框架在OCP上的部署
    Openshift 用户,角色和RBAC
    取消Windows server 关机提示备注的方法
  • 原文地址:https://www.cnblogs.com/refuge/p/10388671.html
Copyright © 2020-2023  润新知