Property of SET is it doesnt allow duplicate elements .
but referring to SCJP: When hashset or linkedhashset is used. when you add objects you must override hashcode else you may end up with duplicate elements in the set.
boolean[] b=new boolean[5];
Set s=new HashSet();
b[0]=s.add("a");
b[1]=s.add("a");
here the output is True , False I wonder how come the output is false when you have not overridden the hashcode. But when you override hashcode, you must overrride equals().
DOes Collection interface provide default equals() method?
I am not able to understand,
First of all,
Stringdoes overridehashCodeandequals, so your code usesString's version of those methods (since you are addingStrings to yourHashSet).Second of all, even if it didn't, in your example, "a" is interned, so both calls to s.add("a") are adding the exact same object, so even
Object's default implementation ofhashCodeandequalswould have given the same results.