Java OCA OCP Practice Question 1579

Question

Suppose we want to store MyClass objects.

Which of the following pairs require MyClass to implement the Comparable interface or create a Comparator in order to add them to the Collection?

  • A. ArrayList and ArrayDeque
  • B. HashMap and HashSet
  • C. HashMap and TreeMap
  • D. TreeMap and TreeSet


D.

Note

TreeMap and TreeSet keep track of sort order when you insert elements.

TreeMap sorts the keys and TreeSet sorts the objects in the set.

This makes Option D correct.

Note that you have the option of having MyClass implement Comparable, or you can pass a Comparator to the constructor of TreeMap or TreeSet.




PreviousNext

Related