栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HashMap中为啥要重写hashcode和equals方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HashMap中为啥要重写hashcode和equals方法

1. equals方法

如果使用==判断俩个对象是否相等,这个只是从地址看是否相等,而与我们的需求是不符合的。即使俩个对象地址是不同的,如果它的属性是相同的,那么可判定这俩个对象相等。

未重写equals方法:

public class Person {


    public static void main(String[] args) {
        Person p1 = new Person();
        Person p2 = new Person();

        System.out.println(p1.equals(p2)); 
    }

    public int no;
    public String name;
   
}

运行结果: false

重写equals方法后:

public class Person {


    public static void main(String[] args) {
        Person p1 = new Person();
        Person p2 = new Person();

        System.out.println(p1.equals(p2));
    }

    public int no;
    public String name;



    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person person = (Person) o;

        if (no != person.no) {
            return false;
        }
        return name != null ? name.equals(person.name) : person.name == null;
    }
}

运行结果: true


2. hashCode方法

由于在hashMap中在put时,散列函数根据它的哈希值找到对应的位置,如果该位置有原素,首先会使用hashCode方法判断,如果没有重写hashCode方法,那么即使俩个对象属性相同hashCode方法也会认为他们是不同的元素,又因为Set是不可以有重复的,所以这会产生矛盾,那么就需要重写hashCode方法。

没有重写hashCode方法

public class Person {


    public static void main(String[] args) {
        Person p1 = new Person();
        Person p2 = new Person();

        HashMap map = new HashMap<>();
        map.put(p1,1);
        map.put(p2,2);

        System.out.println(map.get(new Person()));

    }

    public int no;
    public String name;



    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person person = (Person) o;

        if (no != person.no) {
            return false;
        }
        return name != null ? name.equals(person.name) : person.name == null;

    }

}

运行结果:null

重写了hashCode方法

public class Person {


    public static void main(String[] args) {
        Person p1 = new Person();
        Person p2 = new Person();

        HashMap map = new HashMap<>();
        map.put(p1,1);
        map.put(p2,2);

        System.out.println(map.get(new Person()));

    }

    public int no;
    public String name;



    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person person = (Person) o;

        if (no != person.no) {
            return false;
        }
        return name != null ? name.equals(person.name) : person.name == null;

    }

    @Override
    public int hashCode() {
        int result = no;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

运行结果:2

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号