Several answers regarding overriding equals() and hashCode() describe the need for both and the application of using them as keys in a HashMap in Java. My question is, is the Java KeyStroke class safe to put as a key in a HashMap? Further, so I’m not annoying and continue to ask these questions, how would one find out if a provided class (I think KeyStroke is provided by Swing) is safe to use in this context…i.e. overrides equals() and hashCode()?
thanks in advance!
The way to see if
equalsandhashCodeare overridden is to take a look at the API.hashCodeandequalsare not overridden inKeyStroke, but they are inAWTKeyStroke. In fact,AWTKeyStroke#hashCodestates: