• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine if the classes use Comparable and Comparator?  RSS feed

 
Sreevatsa Turuvekere Laxmi Narayana
Greenhorn
Posts: 10
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following classes uses Comparable and Comparator?
  • Queue
  • TreeSet
  • Stack
  • PriorityQueue


  • In the above question, what does 'uses' mean? Does it mean do above classes implement Comparable and Comparator?
    I know that in order to compare any two elements stored in one of the above classes, we need to make the elements' class to implement one of these - either Comparable or Comparator.
    How to answer the above question?
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since all those classes are collection classes (they hold collections of elements), I guess the question is about if they use Comparable and Comparator with regard to their elements, not if they implement these themselves.

    Look at the API documentation of those four classes to find out if they use Comparable or Comparator on their elements.

    Some collections keep their elements sorted, and some don't. Collections that do, need to be able to compare their elements to know what the sort order is. So, this is also something you can think about when answering the question.
     
    Mohamed Sanaulla
    Bartender
    Posts: 3185
    34
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is this some assignment question? Can you please quote the source of the question?

    Now to answer your query its not which class implements, but it is about which classes uses it to order the elements present in it. For example TreeSet orders the elements present in it. How does it manage the order? Does it use Comparable or Comparator? Please have a look at the Javadoc for each of those classes to identify which of them use Comparable and Comparator.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!