Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which collection class in java breaks the S.O.L.I.D Principle? [closed]

Tags:

java

ooad

I was asked in an interview about which collection breaks the S.O.L.I.D principle??

Can anyone explain which one is it and how?

like image 306
java newbie Avatar asked Jul 24 '14 12:07

java newbie


1 Answers

Java provides a generic collection called IdentityHashMap<K,V>, which breaks Liskov substitution principle - the "L" in SOLID by intentionally violating Map<K,V>'s contract.

Here is a note from the documentation of the IdentityHashMap<K,V> class:

This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.

like image 73
Sergey Kalinichenko Avatar answered Oct 21 '22 08:10

Sergey Kalinichenko