Suppose I have a HashMap M
. I want to call the "get" function on this HashMap, and find the value associated with a given object S
. But I don't have an actual reference to the object S
, so I create a new object S_new
whose contents are identical to the contents of S
. If I call M.get(S_new)
, will that give me the value associated with the key S
?
-
absolutely no, they are not the same instance. – Euclides Aug 08 '17 at 20:38
-
2Short answer - it will work fine if objects are equal and have same hashCode value. – udalmik Aug 08 '17 at 20:39
-
If they hash the same -- yes. If not -- no. Should it be avoided? -- Absolutely – Easton Bornemeier Aug 08 '17 at 20:40
3 Answers
From the documentation for Map#get:
public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. More formally, if this map contains a mapping from a key
k
to a value v such that(key==null ? k==null : key.equals(k))
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)
So as long as the parameter you're passing overrides equals
in such a way that the map key is seen as equivalent, you can use a different instance to retrieve a value from a map.
Also, as @Eugene and others mentioned, for HashMap
you must also override the hashCode
method, and ensure that your instance returns the same value as your key. In general, best practice is to ensure that your equality implementation is symmetric (i.e. A.equals(B)
<==> B.equals(A)
), and values that are equal should have the same hashCode
.

- 18,126
- 5
- 54
- 66
-
In a hash-based map or collection, `hashCode` must also be overridden or equality will never be tested. – Kevin Krumwiede Aug 08 '17 at 20:53
-
The OP asked for `HashMap`, so it's important to have a matching `hashCode()` method as well. – Ralf Kleberhoff Aug 08 '17 at 20:53
-
@nbrooks if you dont override hashcode that entry might never be found at all in the context of HashMap... – Eugene Aug 08 '17 at 20:58
-
Also relevant: https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java – nbrooks Aug 08 '17 at 21:17
Yes, it will as long as the hashcode
and equals
would produce the same exact values. Also notice that get
does not even require the parameter to be T
- it's Object
, so any
type that would fulfill the hashcode and equals would work

- 117,005
- 15
- 201
- 306
-
In practice, it's somewhere between unlikely and impossible that any other type would compatibly implement `equals` and `hashCode`. See [this answer](https://stackoverflow.com/a/18575969/1953590). – Kevin Krumwiede Aug 08 '17 at 20:51
-
@KevinKrumwiede I'll agree. I have not seen a case where something other than `T` would make logical sense to get from a `Map`; still this is allowed by the spec... – Eugene Aug 08 '17 at 20:59
No to get value from map using key doesn't mean that key should be same object which were used while putting.
only thing is that hashcode & equals should be same and that is the reason its mandatory to override Hashcode & equals method if you wanna use your own class object as key .

- 415
- 3
- 13