• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

K&B Bonus Exam, Comparable doubt

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Megha,

I got that!
 
What a show! What atmosphere! What fun! What a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic