HashSet hs = new HashSet();
hs.add(10);
hs.add("sum");
hs.add(10);
hs.add(12.3d);
System.out.println(hs);
1) In the above code how does java know that when the second object is passed, it needs not check it with the first one. And how does it know that when the third object(i.e 10) is passed it needs to call the overridden .equals method of the Integer class to check first and third object.
2)If it calls the .equals method of the object class then both integers have different instances so both should be added, but this isn't the case.
I need the background working of the .equals method in the collection!
Aucun commentaire:
Enregistrer un commentaire