Skip to content

Commit

Permalink
update Java 基础.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhenzero committed Jun 17, 2019
1 parent e0a7629 commit 210693c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/notes/Java 基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ hashCode() 返回散列值,而 equals() 是用来判断两个对象是否等

在覆盖 equals() 方法时应当总是覆盖 hashCode() 方法,保证等价的两个对象散列值也相等。

下面的代码中,新建了两个等价的对象,并将它们添加到 HashSet 中。我们希望将这两个对象当成一样的,只在集合中添加一个对象,但是因为 EqualExample 没有实现 hasCode() 方法,因此这两个对象的散列值是不同的,最终导致集合添加了两个等价的对象。
下面的代码中,新建了两个等价的对象,并将它们添加到 HashSet 中。我们希望将这两个对象当成一样的,只在集合中添加一个对象,但是因为 EqualExample 没有实现 hashCode() 方法,因此这两个对象的散列值是不同的,最终导致集合添加了两个等价的对象。

```java
EqualExample e1 = new EqualExample(1, 1, 1);
Expand Down

0 comments on commit 210693c

Please sign in to comment.