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();
  • 相关阅读:
    深入Vue.js从源码开始(二)
    Vue.js的动态组件模板
    Vue中的methods、watch、computed
    Understand .sync in Vue
    vue程序中组件间的传值方式
    xUtils框架的介绍(一)
    xUtils框架的介绍(二)
    Java如何计算hashcode值
    Java网络编程总结
    深入Java线程管理(五):线程池
  • 原文地址:https://www.cnblogs.com/tangchun/p/9894566.html
Copyright © 2011-2022 走看看