In case of set it always first check the HashCode and the use the equals to compare the objects.
Lion class does not override the hashcode and equals method so two objects of lion class will never be equal until they are not pointing to same object. So there will not be any duplicate lion object but same is not true for String objects.
If you try the below code :
Output will be
See, there are 3 lion objects and 2 String objects Because String override equals method but Lion does not. If you override the equals and hashcode methods in Lion class you will get desired results.
For two objects of a given class to be equal, they must have the same hashcode and meaningful equivalence defined within equal method. The default implementation in Object only gives two objects to be equal if they are of the same type and share the common reference.
For the exam purposes, it is useful to know that StringBuffer doesn't define equal method.