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
⌛
This post has been reserved for your question.
Hey @blockgoblin31! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Specifically looking at the method .getOrDefault uses, .getNode
HashMap does NOT compare keys with "==". It compares their hashes, hence the name
So basically it compares keys using their
hashCode
methodsAh 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?
Yeah, you will have to override this method in your classes
If your are using IntelliJ it can do it for you
Ok Ty
Post Closed
This post has been closed by <@501514065068294154>.