1.不重写hashcode的后果
类中没有重写hashcode方法,所以调用的是Object类的hashcode方法,获取对象地址值
代码展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public class User {
private String username; private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public User() {}
public User(String username) { this.username = username; }
public User(String username, String password) { this.username = username; this.password = password; }
@Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; }
public static void main(String[] args) {
User user1 = new User("123");
User user2 = new User("123");
System.out.println(user1.hashCode()); System.out.println(user2.hashCode());
} }
|
打印展示
2.重写hashcode,但是不重写equals的后果
能用hashcode找到对应索引位,但可能存在冲突,所以会调用equals判断是否相等,类中没有重写equals方法,所以调用的是Object类的equals方法,以地址值判断
代码展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import java.util.Objects;
public class User {
private String username; private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public User() {}
public User(String username) { this.username = username; }
public User(String username, String password) { this.username = username; this.password = password; }
@Override public int hashCode() { return Objects.hash(username, password); }
@Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; }
public static void main(String[] args) {
User user1 = new User("123");
User user2 = new User("123");
System.out.println("user1的hashcode值为: " + user1.hashCode()); System.out.println("user2的hashcode值为: " + user2.hashCode()); System.out.println("user1与user2是否相同: " + user1.equals(user2));
} }
|
打印展示
3.重写hashcode和equals
代码展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.util.Objects;
public class User {
private String username; private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public User() {}
public User(String username) { this.username = username; }
public User(String username, String password) { this.username = username; this.password = password; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(username, user.username) && Objects.equals(password, user.password); }
@Override public int hashCode() { return Objects.hash(username, password); }
@Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; }
public static void main(String[] args) {
User user1 = new User("123");
User user2 = new User("123");
System.out.println("user1的hashcode值为: " + user1.hashCode()); System.out.println("user2的hashcode值为: " + user2.hashCode()); System.out.println("user1与user2是否相同: " + user1.equals(user2));
} }
|
打印展示