美文网首页
equals,hashCode写法

equals,hashCode写法

作者: 盘丝仙仙 | 来源:发表于2019-10-30 16:28 被阅读0次

2019-10-30

原文链接:https://blog.csdn.net/Mrs_chens/article/details/92761868

hashCode()和equals()的区别

下边从两个角度介绍了他们的区别:一个是性能,一个是可靠性。他们之间的主要区别也基本体现在这里。

1.equals()既然已经能实现对比的功能了,为什么还要hashCode()呢?

因为重写的equals()里一般比较的比较全面比较复杂,这样效率就比较低,而利用hashCode()进行对比,则只要生成一个hash值进行比较就可以了,效率很高。

2.hashCode()既然效率这么高为什么还要equals()呢?

因为hashCode()并不是完全可靠,有时候不同的对象他们生成的hashcode也会一样(生成hash值得公式可能存在的问题),所以hashCode()只能说是大部分时候可靠,并不是绝对可靠,所以我们可以得出(PS:以下两条结论是重点,很多人面试的时候都说不出来):

//hashCode和equlas的关系

//1.如果两个对象相同(即用equals比较返回true),那么它们的hashCode值一定要相同;

//2.如果两个对象的hashCode相同,它们并不一定相同(即用equals比较返回false)

当然如果我们只是平时想对比两个对象 是否一致,则只重写一个equals(),然后利用equals()去对比也行的。

所以如果我们的对象要想放进hashSet,并且发挥hashSet的特性(即不包含一样的对象),则我们就要重写我们类的hashCode()和equals()方法了。像String,Integer等这种类内部都已经重写了这两个方法。

class BookEquals3 {

private Stringtitle;

    private double price;

    public BookEquals3(String title, double price) {

this.title = title;

        this.price = price;

    }

@Override

    public boolean equals(Object o) {

if (this == o) {

return true;

        }

if (o ==null || getClass() != o.getClass()) {

return false;

        }

BookEquals3 book3 = (BookEquals3) o;

        //其实Double.compare这样写是有问题的 不能准确的比较 所以变量还是要用封装类

        return Double.compare(book3.price, price) ==0 &&

Objects.equals(title, book3.title);

        //其实是

//    public static int compare(double d1, double d2) {

//        if (d1 < d2)

//            return -1;          // Neither val is NaN, thisVal is smaller

//        if (d1 > d2)

//            return 1;            // Neither val is NaN, thisVal is larger

//

//        // Cannot use doubleToRawLongBits because of possibility of NaNs.

//        long thisBits    = Double.doubleToLongBits(d1);

//        long anotherBits = Double.doubleToLongBits(d2);

//

//        return (thisBits == anotherBits ?  0 : // Values are equal

//                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)

//                1));                          // (0.0, -0.0) or (NaN, !NaN)

//    }

//    public static boolean equals(Object a, Object b) {

//        return (a == b) || (a != null && a.equals(b));

//    }

//    public boolean equals(Object obj) {

//        return (this == obj);

//    }

    }

@Override

    public int hashCode() {

return Objects.hash(title, price);

        //其实是

//    public static int hash(Object... values) {

//        return Arrays.hashCode(values);

//    }

//    public static int hashCode(Object a[]) {

//        if (a == null)

//            return 0;

//

//        int result = 1;

//

//        for (Object element : a)

//            result = 31 * result + (element == null ? 0 : element.hashCode());

//

//        return result;

//    }

    }

@Override

    public StringtoString() {

return "Book{" +

"title='" +title +'\'' +

", price=" +price +

'}';

    }

}

相关文章

网友评论

      本文标题:equals,hashCode写法

      本文链接:https://www.haomeiwen.com/subject/wdtavctx.html