成都创新互联网站制作重庆分公司

equals和hashcode是什么-创新互联

小编给大家分享一下equals和hashcode是什么,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

成都创新互联公司基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业眉山服务器托管报价,主机托管价格性价比高,为金融证券行业服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

equals和hashcode总结:

equals方法没有重写的话,用于判断对象的内存地址引用是否是用一个地址。重写之后一般用来比较对象的内容是否相等(比如student对象,里面有姓名和年龄,我们重写equals方法来判断只要姓名和年龄相同就认为是用一个学生)。

hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值,当然你也可以重写它,hashcode方法只有在集合中用到。

对象放入集合中时,先判断hashcode是否相等,再判断equals是否相等,都相等就算是同一个对象,list则可以放入,set因为不允许重复所以不会放入。

例如:

public class Student {
        private int age;
        private String name; 
        public Student(int age ,String name){
            this.age = age;
            this.name = name;
        }
        public int getAge() {
           return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
           return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        //重写equals方法,判断姓名和年龄相同就是相等的
        public boolean equals(Object o){
            if(o == null){
                return false;
            }
            if(this.getClass() != o.getClass()){
             return false;   
            }
            Student student = (Student)o;
            if(name == null){
                return false;
            }
            if(age==student.getAge()&&name.equals(student.getName())){
                return true;
            }
            return false;
        }       
    public static void main(String[] args) {
        Student studentOne = new Student(1,"yjc");
        Student studentTwo = new Student(1,new String("yjc"));
        System.out.println(studentOne.equals(studentTwo));
        System.out.println("1: "+studentOne.getName().hashCode());
        System.out.println("2: "+studentTwo.getName().hashCode());
    }
    //输出结果:true
                1: 119666
                2: 119666
}

以上可以看出,两个String都叫"yjc",无论是直接"yjc"还是new String("yjc"),他们的hashcode都相同。所以在重写hashcode方法时可以运用这一点。

比如你希望如果姓名和年龄相同,不仅equals相同,他们的hashcode也要相同,可以这样重写hashcode:

public int hashcode(){
    final int prime = 31; 
    int result = 1;    
    result = prime*result + age;    
    result = prime*result + (name == null? 0 : name.hashcode());   
    return result;//直接写age+(name == null? 0 : name.hashcode())也行就是感觉太简单了0.0
}

这样一来两个姓名和年龄相同的Student对象就是同一个对象了,放入set中会被认为是同一个,无论放几个这样的对象,set.size()都是等于1。

同样,HashMap因为key也是唯一的,HashMap对象是根据其Key的hashCode来定位存储位置,并使用equals(key)获取对应的Value,所以在put时判断key是否重复用到了hashcode和equals,若重复了则会覆盖。

看完了这篇文章,相信你对equals和hashcode是什么有了一定的了解,想了解更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:equals和hashcode是什么-创新互联
文章地址:http://cxhlcq.com/article/geoho.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部