specified features *Each element must be unique. *Contains no duplicate elements. *Elements are not key/value pairs. *Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provides the specified features? a. LinkedList b. TreeMap c. TreeSet d. HashMap e. HashSet f. LinkedHashMap g. Hashtable h. None of the above
Answer:e.
Doubt: why can't C be the answer as it fulfills the features required
Since speed is a criteria, it has to be HashSet over TreeSet. The TreeSet stores the items in its natural order, which cannot be changed and which also makes the access slower.
Hi Gitesh, The following three are common to TreeSet and HashSet
*Each element must be unique. *Contains no duplicate elements. *Elements are not key/value pairs.
Now the last feature is where these differ :
Accessing an element can be almost as fast as performing a similar operation on an array.
This feature makes the choice to be HashSet .
If you are wondering why ..then to give you a hint ...
The HashSet is very fast for searching because of the the hashing .Well this is true for any Hash<table , Map , Set > . The elements in the TreeSet are kept in a sorted fashion.