Skip to content

Conversation

@TwilightFlower
Copy link

Implement the hashCode and equals methods on Member. This makes using Member as a key in a HashMap possible.

@liach
Copy link
Contributor

liach commented May 27, 2020

Shall they be used in hash maps, their fields should be final. Implementing these two is incomplete for hashmap usage without making the fields final.

@TwilightFlower
Copy link
Author

TwilightFlower commented May 27, 2020

Ah, hmm, I completely missed that the fields were mutable. Guess I'm just used to most data storage objects like these being immutable.


@Override
public int hashCode() {
return (31 * (31 * owner.hashCode()) + name.hashCode()) + desc.hashCode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use java.util.Objects.hash for that

}

@Override
public boolean equals(Object other) {
Copy link
Contributor

@Marcono1234 Marcono1234 May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should start with if (other == this) { return true; } for better performance for that case, though I don't know if that is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants