The explanation for option C states:
I added some code into the main method as follows:In this particular case, since the keys (i.e. the two Info objects) are unequal as per their equals() method and even though their hashcode are same, the HashMap will work properly for them. Both the objects will be stored and retrieved. However, the retrieval will fail only for Info objects that break the rule of equals() and hashCode() are put in the map.
Code: Select all
System.out.println("nbr of entries in map = " + map.size()); System.out.println("hashcode for i1 = " + map.get(i1).hashCode()); System.out.println("hashcode for i2 = " + map.get(i2).hashCode());
Perhaps I am misunderstanding this, but I see these statements saying the hashcodes are not the same.nbr of entries in map = 2
hashcode for i1 = 99162322
hashcode for i2 = 113318802
Also, can you please clarify the final statement in the explanation box for this question that states:
I can certainly see how the hasCode() values will be different but how will the equals() method return true when adding either of these objects?In this case, equals() will return true for: new Info("aa", "b", "c") and new Info("a", "ab", "c") but their hashCode() values will be different.