How would I compare HashMap keys with .equals() instead of ==?

HashMap.getOrDefault compares keys with == instead of .equals, is there an equivalent method or something else I could use to compare the keys with .equals() instead?(I assumed it was .equals and would have to recode about half my project if there isnt a workaround)
7 Replies
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
blockgoblin31
blockgoblin31OP11mo ago
Specifically looking at the method .getOrDefault uses, .getNode
Defective
Defective11mo ago
HashMap does NOT compare keys with "==". It compares their hashes, hence the name So basically it compares keys using their hashCode methods
blockgoblin31
blockgoblin31OP11mo ago
Ah ok, I saw == and assumed that’s why my code wasn’t working So if I add that to my class it’ll work fine?
Defective
Defective11mo ago
Yeah, you will have to override this method in your classes If your are using IntelliJ it can do it for you
blockgoblin31
blockgoblin31OP11mo ago
Ok Ty
JavaBot
JavaBot11mo ago
Post Closed
This post has been closed by <@501514065068294154>.

Did you find this page helpful?