class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("http://www.itsvse.com");
            HashSet<Test1> list1 = new HashSet<Test1>();
            HashSet<Test2> list2 = new HashSet<Test2>();
            HashSet<string> list3 = new HashSet<string>();             list1.Add(new Test1(1, "a"));
            list1.Add(new Test1(2, "b"));
            list1.Add(new Test1(3, "c"));
            list1.Add(new Test1(4, "d"));
            list1.Add(new Test1(4, "d"));             list2.Add(new Test2(1, "a"));
            list2.Add(new Test2(2, "b"));
            list2.Add(new Test2(3, "c"));
            list2.Add(new Test2(4, "d"));
            list2.Add(new Test2(4, "d"));             list3.Add("1");
            list3.Add("2");
            list3.Add("3");
            list3.Add("4");
            list3.Add("4");             Console.WriteLine("输出list1");
            foreach (var item in list1)
            {
                Console.WriteLine(item.id);
            }             Console.WriteLine("输出list2");
            foreach (var item in list2)
            {
                Console.WriteLine(item.id);
            }             Console.WriteLine("输出list3");
            foreach (var item in list3)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }     public class Test1
    {
        public Test1(long i,string str)
        {
            this.id = i;
            this.a = str;
        }
        public long id { get; set; }
        public string a { get; set; }
    }     public class Test2
    {
        public Test2(long i, string str)
        {
            this.id = i;
            this.a = str;
        }
        public long id { get; set; }
        public string a { get; set; }         public override bool Equals(object obj)
        {
            Test2 e = obj as Test2;
            return this.id == e.id && this.a == e.a;
        }         public override int GetHashCode()
        {
            return this.id.GetHashCode() + this.a.GetHashCode();
        }
    }

  

1,如果hash码值不相同,说明是一个新元素,存;

2,如果hash码值相同,且equles判断相等,说明元素已经存在,不存;

3,如果hash码值相同,且equles判断不相等,说明元素不存在,存;

我们Test2对象,重写了对象的的equals和hashCode方法。这里让Test2对象,只要是id和a相同就认为是相同的实例,当然也可以是其他,这就要看具体需求

HashSet去重的更多相关文章

  1. List通过HashSet去重

    //JAVA中//使用hashset去重复,set为重复的集合,可以通过new ArrayList(set)转换成list HashSet<HashMap<String, String&g ...

  2. java 集合 HashSet 实现随机双色球 HashSet addAll() 实现去重后合并 HashSet对象去重 复写 HashCode()方法和equals方法 ArrayList去重

    package com.swift.lianxi; import java.util.HashSet; import java.util.Random; /*训练知识点:HashSet 训练描述 双色 ...

  3. java中List对象列表去重或取出以及排序

    面试碰到几次list的去重和排序.下面介绍一种做法: 1. list去重 1.1 实体类Student List<Student>容量10k以上,要求去重复.这里Student的重复标准是 ...

  4. HashSet中的元素必须重写equals方法和hashCode方法

    http://jingyan.baidu.com/article/d5a880eb8fb61d13f147cc99.html 1.为什么必须重写这两个方法. 2.什么事hashSet去重,符合什么样的 ...

  5. List对象去重碎碎念之神叨叨

    前言 List集合操作去除重复数据的这种情况经常会碰到,博客园里面也有很多大神们做过,在这里主要是借鉴然后自己整理了一下,主要是为了方便自己,以后再次碰到这种去重问题,直接打开自己的链接拿起键盘就是干 ...

  6. List集合去重的一些方法(常规遍历、Set去重、java8 stream去重、重写equals和hashCode方法)

    1. 常规元素去重 碰到List去重的问题,除了遍历去重,我们常常想到利用Set集合不允许重复元素的特点,通过List和Set互转,来去掉重复元素. // 遍历后判断赋给另一个list集合,保持原来顺 ...

  7. java 数组排序并去重

    https://www.cnblogs.com/daleyzou/p/9522533.htmlimport java.lang.reflect.Array;import java.util.Array ...

  8. List转换字典去重问题

    数据源 var list = new List<TestClass> { ,Name="}, ,Name="}, ,Name="}, ,Name=" ...

  9. C#黔驴技巧之去重(Distinct)

    前言 关于C#中默认的Distinct方法在什么情况下才能去重,这个就不用我再多讲,针对集合对象去重默认实现将不再满足,于是乎我们需要自定义实现来解决这个问题,接下来我们详细讲解几种常见去重方案,孰好 ...

随机推荐

  1. Spring项目配置多数据源

    项目中有用到多数据源,并进行动态切换,使用的是阿里的druid.看网上有一篇大致一样的就偷偷懒 import java.sql.SQLFeatureNotSupportedException; imp ...

  2. Django使用Mysql已存在数据表的方法

    在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...

  3. intellij idea 新建springboot工程pom.xml报错

    今天使用idea新建的springboot工程pom.xml文件报错如下 1. 问题 'settings.xml' has syntax errors less... (Ctrl+F1) Inspec ...

  4. 【转】Pandas学习笔记(一)基本介绍

    Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...

  5. 关于std::bind的文章收集

    C++11 FAQ中文版:std::function 和 std::bind 2011-03-02 16:25 by 陈良乔 常规性地介绍了function和bind的使用,还不会用的同学可以看看 b ...

  6. 借助模板类自动实现COM连接点接收器(Sink)更新

    之前在借助模板类自动实现COM连接点接收器(Sink)中对原作者的代码进一步封装,弄清了连接点使用的原理,在看ATL代码的过程中,发现ATL本身就提供了AtlAdvise/AtlUnadvise这样的 ...

  7. SpringBoot——配置文件详解【五】

    前言 SpringBoot的配置文件 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application.properties application.yml 配置文件 ...

  8. RabbitMQ六种队列模式-工作队列模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  9. python 识别二维码内容的方法

    识别二维码链接的方式有多种,那么如何用python 的方法实现识别呢? 请看如下代码: from pyzbar.pyzbar import decode from PIL import Image i ...

  10. [RN] React Native 实现 多选标签

    React Native 实现 多选标签 效果如下: 实现代码: import React, {Component} from 'react'; import {Button, StyleSheet, ...