Any downside of using arbitrary objects as map keys in Java?

I have two types of objects in my application, where each object of one kind has exactly one corresponding object of another type.

The obvious choice to track this relationship is Map<type1, type2> , like HashMap. But for some reason I'm suspicious. Can I use the object as a key on the card, transfer it, go to another collection and at any time get its partner from the Card?

After creating the object, everything I look at is an identifier, right? So, probably, there are no problems. What if i serialize and deserialize the key?

Any other reservations? Should I use something else to correlate pairs of objects, like a number that I myself generate?

+6
java collections maps
source share
8 answers
  • The key should correctly implement .equals() and .hashCode()
  • The key should not be changed in any way that changes its .hashCode() value when it is used as a key
  • Ideally, any object used as a key in a HashMap should be immutable. This will automatically ensure that 2. is always considered true.
  • Objects that might otherwise be GCed can be stored around when used as a key and / or value.
+22
source share

I have two kinds of objects in my application, where each object of one kind has exactly one corresponding object of another kind.

It really looks like a has-a relationship and therefore can be implemented using a simple attribute.

+7
source share

It depends on the implementation of your chosen map:

  • HashMap uses equals () and hashCode () . By default (in Object) they are based on the identifier of the object, which will work fine if you do not serialize / deserialize. If you correctly implement equals () and hashCode () based on the contents of the object, you will not have problems if you do not modify it while it is the key in the hash map.

  • TreeMap uses compareTo () . There is no default implementation, so you need to provide one. The same restrictions apply to the implementation of hashCode () and equals () above.

+3
source share

You can use a standard map, but at the same time you will save links to your objects on the map. If your objects are referenced in a different structure and you need a map to link them together, consider using the WeakHashMap file.

And BTW you do not need to redefine equals and hashCode if you do not need to consider several object instances as equal ...

+1
source share

Can I use the object as a key on the Map, transfer it, make it sit in another collection and extract its partner from the Map at any time?

Yes, there is no problem.

After creating the object, everything I look at is an identifier, right? So, probably, there are no problems. What if i serialize and deserialize the key?

That's right, you only pass the link - they all point to the same actual object. If you serialize or deserialize an object, this will create a new object. However, if your object implements equals and hashCode correctly, you can still use the new deserialized object to extract elements from the map.

Any other reservations? Should I use something else to correlate pairs of objects, like a number that I myself generate?

As with Caveats, yes, you cannot change anything that will change the hash code of the object when the object is on the Map.

+1
source share

Any object can be a card key. It is important to make sure that you override .equals () and .hashCode () for any objects that will be used as map keys.

The reason you do this is because if you do not, equal will be understood as equality of objects, and the only way you can find "equal" map keys is to have a handle to the source object itself.

You are redefining the hash code because it must be compatible with peers. This means that the objects that you define as equal hash are identical.

0
source share

Failure points are hash code and equal functions. If they do not give consistent and correct return values, the Card will behave strangely. Effective Java contains the entire section and is highly recommended.

0
source share

You may consider the Google Collection BiMap .

0
source share

All Articles