InterviewSolution
| 1. |
Why We Use Set Interface? What Are Main Classes Implementing Set Interface? |
|
Answer» It models the mathematical set in set theory. Set interface is like List interface but with some differences. First, it is not ordered collection.So no ordering is preserved while adding or removing elements. The main feature it does provide is “uniqueness of elements“. It does not SUPPORT duplicate elements. Set also adds a stronger CONTRACT on the behavior of the EQUALS and hashCode OPERATIONS, allowing Set instances to be compared meaningfully even if their implementation TYPES differ. Two Set instances are equal if they contain the same elements. Based on above reasons, it does not have operations based on indexes of elements like List. It only has methods which are inherited by Collection interface. Main classes implementing Set interface are : EnumSet, HashSet, LinkedHashSet, TreeSet. It models the mathematical set in set theory. Set interface is like List interface but with some differences. First, it is not ordered collection.So no ordering is preserved while adding or removing elements. The main feature it does provide is “uniqueness of elements“. It does not support duplicate elements. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements. Based on above reasons, it does not have operations based on indexes of elements like List. It only has methods which are inherited by Collection interface. Main classes implementing Set interface are : EnumSet, HashSet, LinkedHashSet, TreeSet. |
|