Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
24 lines (19 loc) · 537 Bytes

File metadata and controls

24 lines (19 loc) · 537 Bytes
Copy raw file
Download raw file
Edit and raw actions

Don't implement #equals using just a hashCode comparison:

class MyClass {
  private final int a;
  private final int b;
  private final String c;

  ...

  @Override
  public boolean equals(@Nullable Object o) {
    return o.hashCode() == hashCode();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(a, b, c);
  }

The number of Objects with randomly distributed hashCode required to give a 50% chance of collision (and therefore, with this pattern, erroneously correct equality) is only ~77k.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.