Java - Interview Questions and Answers on Set

Q1.  What is the difference between List, Set and Map ?

Ans. List - Members are stored in sequence in memory and can be accessed through index. 
Set - There is no relevance of sequence and index. Sets doesn't contain duplicates whereas multiset can have duplicates. Map - Contains Key , Value pairs.

Q2.  What is a Set ? What is a Set Interface ?

Ans. It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Q3.  Can we use Ordered Set for performing Binary Search ?

Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets.

Q4.  What is comparator interface used for ?

Ans. The purpose of comparator interface is to compare objects of the same class to identify the sorting order. Sorted Collection Classes ( TreeSet, TreeMap ) have been designed such to look for this method to identify the sorting order, that is why class need to implement Comparator interface to qualify its objects to be part of Sorted Collections.

Q5.  Which are the sorted collections ?

Ans. TreeSet and TreeMap