Sar-E-Bazar Niklon To Awargi Ka Ilzaam,,,
Tanhai Mein Baith Jao T0u Ilzam-E-Muohabbat
Java Interview Questions & Answers: user defined key class
This is one of my favorite core Java interview questions as not knowing this can cause subtle and intermittent issues in Java. The issues that arise from not understanding equals( ) and hashCode( ) contract can be hard to debug. Q. When providing a user defined key class for storing objects in a Map (e.g. HashMap) , what methods do you have to provide or override (i.e. method overriding)? A. You should override the equals( ... ) and hashCode( .. ) methods from the Object class. The default implementation of the equals( ... ) and hashCode( ... ) methods are inherited from the java.lang.Object class uses an object instance’s memory location (e.g. MyObject@6c60f2ea). This can cause problems when two instances of car objects have the same color but the inherited equals( .. ) will return false because it uses the memory location as oppose to the color, which is different ...
Comments
Post a Comment