williamfiset / Algorithms

A collection of algorithms and data structures
MIT License
16.84k stars 4.29k forks source link

Bad Implementations of `equals()` Throw ClassCastException #394

Closed syoon2 closed 11 months ago

syoon2 commented 11 months ago

https://github.com/williamfiset/Algorithms/blob/b25b0a33562100a23dab3027fac76cf529266ab2/src/main/java/com/williamfiset/algorithms/datastructures/hashtable/DoubleHashingTestObject.java#L89-L96

https://github.com/williamfiset/Algorithms/blob/b25b0a33562100a23dab3027fac76cf529266ab2/src/test/java/com/williamfiset/algorithms/datastructures/set/HSetTest.java#L27-L30

The current implementations of the overridden equals() methods assume that the argument is of the correct type. This assumption is quite dangerous because if the argument is not of the correct type, then casting to the expected type immediately throws a java.lang.ClassCastException.