Querz / NBT

A java implementation of the NBT protocol, including a way to implement custom tags.
MIT License
182 stars 48 forks source link

NBT classes do not implement hashCode when implementing equals #15

Closed Marcono1234 closed 5 years ago

Marcono1234 commented 5 years ago

The NBT classes implement equals, but not hashCode. The java-doc says that both should be implemented:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Querz commented 5 years ago

You're right, here's a pull request that fixes this: #17

equals() works by actually checking all keys and values, this means hashCode() needs to do the same to comply with the java-doc (which might cause some performance issues when used on huge NBT structures). The implementation of hashCode() does currently not affect the NBT implementation, because the only thing that calculates hash codes is the CompoundTag which is using a HashMap which in turn only ever has Strings as keys, not Tags.