zoukankan      html  css  js  c++  java
  • List去重的实现

    List<T> 当T为值类型的时候 去重比较简单,当T为引用类型时,一般根据业务需要,根据T的中几个属性来确定是否重复,从而去重。

    查看System.Linq下的Enumerable存在一个去重方法

        /// <summary>Returns distinct elements from a sequence by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</summary>
            /// <param name="source">The sequence to remove duplicate elements from.</param>
            /// <param name="comparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</param>
            /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
            /// <returns>An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains distinct elements from the source sequence.</returns>
            /// <exception cref="T:System.ArgumentNullException">
            ///         <paramref name="source" /> is <see langword="null" />.</exception>
            [__DynamicallyInvokable]
            public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
            {
                if (source == null)
                {
                    throw Error.ArgumentNull("source");
                }
                return DistinctIterator(source, comparer);
            }

    通过实现IEqualityComparer<T>比较器来实现对象的比较。

    IEqualityComparer<T>的简单实现,通过委托来比较对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Extensions
    {
        public delegate bool ComparerDelegate<T>(T x, T y);
    
        /// <summary>
        /// list比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class ListCompare<T> : IEqualityComparer<T>
        {
            private ComparerDelegate<T> _comparer;
    
            public ListCompare(ComparerDelegate<T> @delegate)
            {
                this._comparer = @delegate;
            }
    
            public bool Equals(T x, T y)
            {
                if (ReferenceEquals(x, y))
                {
                    return true;
                }
                if (_comparer != null)
                {
                    return this._comparer(x, y);
                }
                else
                {
                    return false;
                }
            }
    
            public int GetHashCode(T obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    }

    使用方法:

    list= List.Distinct(new ListCompare<Path>
                    ((x, y) => x.Latitude == y.Latitude && x.Longitude == y.Longitude)).ToList();
  • 相关阅读:
    (二)unittest之跳过测试和预期失败的用例
    (一)基本使用
    python从入门到入魔
    (十)中断测试
    (九)进程测试
    (八)安装、卸载与升级更新测试
    (七)功能测试
    (六)内存分析,待补充
    (五)跑Monkey过程中出现的ANR问题分析
    (四)一种精准monkey测试的方法
  • 原文地址:https://www.cnblogs.com/tangchun/p/9894566.html
Copyright © 2011-2022 走看看