Since the set structure (note that your hash file is supported by the set) does not allow you to save two identical objects. This is how they behave.
Now you might be fooled into thinking that both a1 and a2 are equal, but if they do not override equals or hashCode , then for Java they are not equal. However, with your s1 and s2 strings, they are really equal, because the String implementation already overrides the equals and hashCode methods. Try making s1.equals(s2) and you will get the result true . If you execute a1.equals(a2) , you will get false .
At the end, your hashset contains a1, a2, and s1.
You have expanded your question to answer this question ...
s1 and s2 do not belong to the same object, they are two different String objects, but both represent the same character set. Since they are not the same object, System.out.println(s1 == s2) prints false . They are equal() , but not the same object.
Cesar source share