1.What is the Set interface ?
- The Set interface provides methods for accessing the elements of a finite mathematical set
- Sets do not allow duplicate elements
- Contains no methods other than those inherited from Collection
- It adds the restriction that duplicate elements are prohibited
- Two Set objects are equal if they contain the same elements
The main implementations of the List interface are as follows:
- A HashSet is an unsorted, unordered Set.
- It uses the hashcode of the object being inserted (so the more efficient your hashcode() implementation the better access performance you’ll get).
- Use this class when you want a collection with no duplicates and you don’t care about order when you iterate through it.
TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time.
5.What is an EnumSet ?
An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.
6.Difference between HashSet and TreeSet ?
|HashSet is under set interface i.e. it does not guarantee for either sorted order or sequence order.||TreeSet is under set i.e. it provides elements in a sorted order (acceding order).|
|We can add any type of elements to hash set.||We can add only similar types|
of elements to tree set.
JAVA INTERVIEW QUESTIONS - Map interface