Skip to content

Commit

Permalink
Modern hashCode() and equals().
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 27, 2023
1 parent 5c1e0ae commit 41f7b90
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -62,12 +63,15 @@ private static class MapEntry<K> implements Map.Entry<K, Object> {
}

@Override
public boolean equals(final Object o) {
if (!(o instanceof Map.Entry)) {
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Map.Entry)) {
return false;
}
final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
return key.equals(e.getKey()) && (value == null ? e.getValue() == null : value.equals(e.getValue()));
final Map.Entry<? ,?> other = (Map.Entry<? ,?>) obj;
return Objects.equals(key, other.getKey()) && Objects.equals(value, other.getValue());
}

@Override
Expand All @@ -82,7 +86,7 @@ public Object getValue() {

@Override
public int hashCode() {
return key.hashCode() + (value == null ? 0 : value.hashCode());
return Objects.hash(key, value);
}

@Override
Expand Down

0 comments on commit 41f7b90

Please sign in to comment.