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. php dirname 的简单使用

    dirname dirname-返回路径中的目录部分 说明 dirname(string$path) :string 给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名. 参数 ...

  2. Shell 编程 函数

    本篇主要写一些shell脚本函数的使用. 函数调用 #!/bin/bash sum(){ s=`expr 2 + 3` echo $s } sum [root@localhost ~]# vim su ...

  3. 第一部分day1-变量、运算

    变量:为了存储程序运算过程中的一些中间 结果,为了方便日后调用常量:固定不变的量,字符大写 变量的命名规则 1.字母数字下划线组成2.不能以数字开头,不能含特殊字符和空格3.不能以保留字命名4.不能以 ...

  4. Nginx常见的安装方式

    Nginx常见的安装方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx概述 Nginx的安装版本分为开发版.稳定版和过期版, Nginx安装可以使用yum或源码安装 ...

  5. @TableField

    @TableField 描述:字段注解(非主键) 属性 类型 必须指定 默认值 描述 value String 否 "" 字段名 el String 否 "" ...

  6. 201871010110-李华《面向对象程序设计(java)》第四周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  7. 网络编程 UDP协议 TCP局域网客户端与服务端上传下载电影示例

    UDP协议 (了解) 称之为数据包协议,又称不可靠协议. 特点: 1) 不需要建立链接. 2) 不需要知道对方是否收到. 3) 数据不安全 4) 传输速度快 5)能支持并发 6) 不会粘包 7) 无需 ...

  8. mysql解析binlog日志

    binlog日志用于记录所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句.语句以“事件”的形式保存,它描述数据更改.因为有了数据更新的binlog,所以可以用于 ...

  9. ajax中什么时候进success和error

    先简单介绍下ajax: 简单的说Ajax请求通过XMLHttpRequest对象发送请求,该对象有四个状态(readyState): -未初始化.-正在初始化.-发送数据.-正在发送数据.-完成. 当 ...

  10. bootstrap基础自我总结

    *今天自学了一些封装的css,看起来官网很强大,但是因为源码备注都是英文的情况下,还是感觉想深入有点力不从心,发现度娘没有中文手册,无奈,不过还好代码无国界,基本都是能看懂的,备注也影响不大(安慰自己 ...