equals和hashcode
==
- 对于基本数据来说 ==比较的是值
- 对于引用类型来说 ==比较的是对象的内存地址
equals
判断俩个对象是否相等,存在于Object类中,所有的对象都有equals方法
//Indicates whether some other object is "equal to" this one.
public boolean equals(Object obj) {
return (this == obj);
}
package com.jasper.equalsAndHashcode;
public class demo {
public static void main(String[] args) {
String abc = new String("abc");
String abc1 = new String("abc");
System.out.println(abc.equals(abc1));
System.out.println(abc == abc1);
}
}
output:
true
false
-
类没有从写equals方法时 比较俩个对象等价于通过==比较俩个对象
-
类重写了equals方法时: 一般都时重写equal方法来比较俩个对象中的属性是否相等,属性相等,则返回true String中从写equals方法
-
String源码
String JDK8 public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
hashCode
/**
*Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
*/
@IntrinsicCandidate
public native int hashCode();
- 获取哈希码(int 正数)散列码,有确定对象在哈希表中的索引位置的作用
- 本地方法c或者c++实现的
散列表存储的是键值对(key-value),它的特点是:能根据“键”快速的检索出对应的“值”
当你把对象加入
HashSet
时,HashSet
会先计算对象的hashCode
值来判断对象加入的位置,同时也会与其他已经加入的对象的hashCode
值作比较,如果没有相符的hashCode
,HashSet
会假设对象没有重复出现。但是如果发现有相同hashCode
值的对象,这时会调用equals()
方法来检查hashCode
相等的对象是否真的相同。如果两者相同,HashSet
就不会让其加入操作成功。如果不同的话,就会重新散列到其他位置。这样我们就大大减少了equals
的次数,相应就大大提高了执行速度
- hash值相等 俩个对象不一定相等(哈希碰撞)
- hashcode相等并且equals方法也返回true则这俩个对象才相等
- 如果hashcode不相等,则可以直接认为这俩个对象不相等
重写equals一定要重写hashcode
因为equals方法判断了俩个对象是相等的 那么他们的hashcode一定也要相等
如果重写了equals但是没有重写hashcode那么可能导致equal方法判断的是俩个相等的对象但是hashcode不相等
重写了equals但是没有写hashcode发现在hashset俩个相等的对象也加进去了
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import java.util.Objects;
@AllArgsConstructor
@ToString
public class Person {
private String name;
private Integer age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(age, person.age);
}
}
import java.util.HashSet;
public class DemoHash {
public static void main(String[] args) {
Person p1 = new Person("aaa", 11);
Person p2 = new Person("aaa", 11);
Person p3 = new Person("bbb", 11);
HashSet<Person> set = new HashSet<>();
set.add(p1);
set.add(p2);
set.add(p3);
System.out.println(p1.equals(p2));
System.out.println(set);
}
}
output:
true
[Person(name=aaa, age=11), Person(name=aaa, age=11), Person(name=bbb, age=11)]
@Override
public int hashCode() {
int nameHash = name.toUpperCase().hashCode();
return nameHash ^ age;
}
import java.util.HashSet;
public class DemoHash {
public static void main(String[] args) {
Person p1 = new Person("aaa", 11);
Person p2 = new Person("aaa", 11);
Person p3 = new Person("bbb", 11);
Person p4 = new Person("AAA", 11);
HashSet<Person> set = new HashSet<>();
set.add(p1);
set.add(p2);
set.add(p3);
set.add(p4);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p4));
System.out.println("p1 = " + p1.hashCode());
System.out.println("p4 = " + p4.hashCode());
System.out.println(set);
}
}
//可以看到内容相等但是没有加入到hashset中
//p1 和p4 hashcode是一样的 但是equals方法判断的不相等 所以没有加进去
true
false
p1 = 64554
p4 = 64554
[Person(name=bbb, age=11), Person(name=aaa, age=11), Person(name=AAA, age=11)]