Why does a method claim return SET over HASHSET?
SET is a interface, HASHSET is a implementation of SET.
1. A Set represents a mathematical set.
2. It is a Collection that, unlike List, does not allow duplicates.
3. There must not be two elements of a Set, say e1 and e2, such that e1.equals(e2).
4. The add method of Set returns false if you try to add a duplicate element.
5. HashSet allows at most one null element.
6. HashSet is faster than other implementations of Set, TreeSet and LinkedHashSet.
When claim return type, it is always better to use the interface type, for easier to change to a different implementation as needed.


Leave a Reply