Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Bonus Exam, Comparable doubt

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


You can use java.lang.Comaprable and java.util.Comparator to
sort collections whole elements are of any valid java type,
as long as all of the collection's elements are of the same class.


This is declared as wrong option.
The reason is given below:

It is incorrect because if a class is final and does not alreadt
implements Comparable, then Comparable can't be used to sort that
class.

I don't understand what is that talking about.

Please guide.

Thanks,
 
megha joshi
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

Its becuase..to sort elements of a class using Comparable, the implementation of the class has to be modified to include CompareTo() method.

So if the class doesnt implement Comparable and its final....than we can't add CompareTo() to it right...

So it can only be sorted using Comparator...as Comparator can be implemented by a external class rather than the class itself.

As the statement uses the word "and" its false...if it would have used "or" it will be true.

(I know I gave such an answer before about a statement using word "creates" ..so )I might not be right here tooo....
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Megha,

I got that!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic