Consider the following class:
import java.util.*;
public class Info
{
String s1, s2, s3;
public Info(String a, String b, String c)
{
s1=a; s2=b; s3=c;
}
public boolean equals(Object obj)
{
if(! (obj instanceof Info) ) return false;
Info i = (Info) obj;
return (s1+s2+s3).equals(i.s1+i.s2+i.s3);
}
public int hashCode()
{
return s1.hashCode();
}
public static void main(String[] args)
{
HashMap map = new HashMap();
Info i1 = new Info("aaa", "aaa", "aaa");
Info i2 = new Info("aaa", "bbb", "ccc");
map.put(i1, "hello"); //1
map.put(i2, "world"); //2
}
}
Which of the following statements are correct regarding the above class?
Select 1 correct option.
a This is an invalid implementation of hashCode() method with respect to the given equals() method.
b Only one of the Info objects will be stored in the HashMap.
c Both the objects will be stored in the HashMap.
d An exception will be thrown at run time at line //2.
Answer: a
I can't understand the reason for the output. Plz explain.