【C#】list 去重(转载)

【C#】list 去重(转载)一、查阅文档Enumerable.Distinct方法是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组、集合中的重复元素,还可以自定义去重的规则。有两个重载方法:////摘要://通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。////参数://source://要从中移除重复元素的序列。.

大家好,又见面了,我是你们的朋友全栈君。

一、查阅文档

Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组、集合中的重复元素,还可以自定义去重的规则。

有两个重载方法:

//
        // 摘要: 
        //     通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。
        //
        // 参数: 
        //   source:
        //     要从中移除重复元素的序列。
        //
        // 类型参数: 
        //   TSource:
        //     source 中的元素的类型。
        //
        // 返回结果: 
        //     一个 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重复元素。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     source 为 null。
        public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source);
        //
        // 摘要: 
        //     通过使用指定的 System.Collections.Generic.IEqualityComparer<T> 对值进行比较返回序列中的非重复元素。
        //
        // 参数: 
        //   source:
        //     要从中移除重复元素的序列。
        //
        //   comparer:
        //     用于比较值的 System.Collections.Generic.IEqualityComparer<T>。
        //
        // 类型参数: 
        //   TSource:
        //     source 中的元素的类型。
        //
        // 返回结果: 
        //     一个 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重复元素。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     source 为 null。
        public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);

第一个方法不带参数,第二个方法需要传一个System.Collections.Generic.IEqualityComparer<T>的实现对象

二、简单实现

1.值类型元素集合去重

List<int> list = new List<int> { 1, 1, 2, 2, 3, 4, 5, 5 };
list.Distinct().ToList().ForEach(s => Console.WriteLine(s));

执行结果是:1 2 3 4 5

注意

.ForEach(s => Console.WriteLine(s));
这段代码仅为了控制台输出显示,实际应用中应该删除。

2.引用类型元素集合去重

首先自定义一个Student类

public class Student
    {
        public string Name { get; private set; }
        public int Id { get; private set; }
        public string Hobby { get; private set; }
        public Student(string name, int id, string hobby)
        {
            this.Name = name;
            this.Id = id;
            this.Hobby = hobby;
        }
        /// <summary>
        /// 方便输出,重写ToString方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0}\t{1}\t{2}", this.Name, this.Id, this.Hobby);
        }
    }

1)不带参数的Distinct方法去重

List<Student> list = new List<Student>() { 
                new Student("James",1,"Basketball"),
                new Student("James",1,"Basketball"),
                new Student("Kobe",2,"Basketball"),
                new Student("Curry",3,"Football"),
                new Student("Curry",3,"Yoga")
            };
            list.Distinct().ToList().ForEach(s => Console.WriteLine(s.ToString()));

执行结果:

【C#】list 去重(转载)

可见,并没有去除重复的记录。

不带comparer参数的Distinct方法是使用的IEqualityComparer接口的默认比较器进行比较的,对于引用类型,默认比较器比较的是其引用地址,程序中集合里的每一个元素都是个新的实例,引用地址都是不同的,所以不会被作为重复记录删除掉。

2)带参数的Distinct方法去重

新建一个类,实现IEqualityComparer接口。注意GetHashCode方法的实现,只有HashCode相同才会去比较

只有一个比较条件

public class Compare:IEqualityComparer<Student>
    {
        public bool Equals(Student x,Student y)
        {
            return x.Id == y.Id;//可以自定义去重规则,此处将Id相同的就作为重复记录,不管学生的爱好是什么
        }
        public int GetHashCode(Student obj)
        {
            return obj.Id.GetHashCode();
        }
    }

然后调用

list.Distinct(new Compare()).ToList().ForEach(s => Console.WriteLine(s.ToString()));

执行结果:

【C#】list 去重(转载)

我们按照Id去给这个集合去重成功!

二、复杂应用

1只有一个比较条件,比较稳妥的写法:

新建一个对象:

public class Product
{  public string Id {get;set}
    public string Name { get; set; }
    public int Code { get; set; }
}

集合

Product[] list = { 
     new Product { Name = "apple", Code = 9 ,ID=1}, 
     new Product { Name = "orange", Code = 4  ,ID=2} 
     new Product { Name = "orange", Code = 4  ,ID=3} 
     new Product { Name = "orange", Code = 4  ,ID=3} 
};
//如果对象存在唯一主键,例如:从数据库里查询出来的数据存在 ID

class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {
       
        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.ID == y.ID;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;


        //Get hash code for the Code field.
        int hashID = product.ID.GetHashCode();

        //Calculate the hash code for the product.
        return hashID;
    }

}

调用

list.Distinct(new ProcuctComparar()).ToList()

2多个比较条件写法

// 如果存在组合主键或组合唯一索引,即多个字段组合才能确定唯一性。 这里是Code和Name
// Custom comparer for the Product class
class ProductComparer2 : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {
       
        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null.
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }

}

调用

list.Distinct(new ProcuctComparar2()).ToList()

三、如何编写一个具有扩展性的去重方法

public class Compare<T, C> : IEqualityComparer<T>
    {
        private Func<T, C> _getField;
        public Compare(Func<T, C> getfield)
        {
            this._getField = getfield;
        }
        public bool Equals(T x, T y)
        {
            return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
        }
        public int GetHashCode(T obj)
        {
            return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
        }
    }
    public static class CommonHelper
    {
        /// <summary>
        /// 自定义Distinct扩展方法
        /// </summary>
        /// <typeparam name="T">要去重的对象类</typeparam>
        /// <typeparam name="C">自定义去重的字段类型</typeparam>
        /// <param name="source">要去重的对象</param>
        /// <param name="getfield">获取自定义去重字段的委托</param>
        /// <returns></returns>
        public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
        {
            return source.Distinct(new Compare<T, C>(getfield));
        }
    }

调用
1单个比较条件
list.MyDistinct(s=>s.Id).ToList();
Id 是用于较的属性(或字段),它是可以是任何一个属性。
2多个比较条件如何实现呢?

思路: list.MyDistinct(s=>s.Id&&s.Name).ToList();

 

 四、拓展应用:

 C#  List 集合 交集、并集、差集、去重,都是实现接口IEqualityComparer

 https://www.cnblogs.com/hao-1234-1234/p/10408602.html

 用到了泛型、委托、扩展方法等知识点。可以用于任何集合的各种去重场景

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141574.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • String类型转换BigDecimal、Date类型

    String类型转换BigDecimal、Date类型String类型转换BigDecimal类型public static void main(String[] args) {         String str1="2.30";            BigDecimal bd=new BigDecimal(str1);            System.out.println(bd);    }Java String类型转换成D…

    2022年6月13日
    38
  • winform使用SplitContainer控件[通俗易懂]

    winform使用SplitContainer控件[通俗易懂]在Windows资源管理器中,当把鼠标指针移动到TreeView控件和ListView控件之间时,可以左右拖动鼠标调整TreeView控件和ListView控件在主窗口中的大小比例,以适应不同显示内容的需要。我们可以使用SplitContainer控件实现这种功能。  可

    2022年7月18日
    96
  • Eclipse SVN冲突详细解决方案「建议收藏」

    Eclipse SVN冲突详细解决方案「建议收藏」大家一起开发,难免有时会同时修改同一个文件,这样就要学会解决冲突。当大家更新代码,发现以下情况的时候,就说明你的修改的文件和服务器的文件产生了冲突(一般是别人也改了同一个文件)。1)和服务器有冲突的文件:2)点击Update以后,如果出现以下情况(出现四个文件),就说明需要解决冲突。如何解决冲突:出现文件冲突的时候:你有四个选择:1以我修改的为准,不管服务

    2022年10月14日
    5
  • j2ee是什么,包括哪些技术_什么是J2EE

    j2ee是什么,包括哪些技术_什么是J2EEjava自学网www.java7.com从整体上讲,J2EE是使用Java技术开发企业级应用的工业标准,它是Java技术不断适应和促进企业级应用过程中的产物。适用于企业级应用的J2EE,提供一个平台独立的、可移植的、多用户的、安全的和基于标准的企业级平台,从而简化企业应用的开发、管理和部署。J2EE是一个标准,而不是一个现成的产品。  主要包括以下这些技术:  1)Servlet  Servlet是Java平台上的CGI技术。Servlet在服务器端运行,动态地生成Web页面。与传统的CGI和..

    2022年10月11日
    2
  • required属性的作用_required的作用

    required属性的作用_required的作用1,required属性-表示字段不能为空(注意:只有用户单击“提交”按钮提交表单的时候,浏览器才会执行验证。目前HTML5不支持指定验证的时间,而且验证消息的样式和内容各个浏览器不大一样,不能修

    2022年8月3日
    9
  • Java动态代理实现动态爬虫

    Java动态代理实现动态爬虫笔者公司是一家区块链门户网站,该网站的很多资讯,快讯,视频等数据都是通过爬虫爬取得第三方网站获得的,需要从很多网站要爬取数据,如果每个数据源网站都需要单独写个接口去爬的话,工作量无疑是巨大的,因为笔者想到了通过动态代理实现一套爬虫机制,每次要爬取新的数据源,只要在数据库里增加一条数据源即可,无需修改代码。废话不多说,下贴出数据库表结构DROPTABLEIFEXISTS…

    2022年7月15日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号