• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Difference between a Comparator &Comparable

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,
What is natural order & total order of ordering elements in relation of Collection interfaces(Sorted maps & sets)?
I have read somewhere that natural order is the ascending order. Am I correct?
Also while going through the API and Khalid & Mughal's book I am confused with concept of comparable(sorts the elements by natural order) and comparator(sorts elements by total order).What is exact difference between the two?

Please help me to clear this confusion.
Thanks in advance,
Trupti.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of them if implemented as the API says imposes a total and ascending order. Total means that is possible to order every pair of objects of a class that implements them.
A class that implements Comparable is said to have a natural order. I suppose that means you do not need anything else to order its objects.
If you want to have more than one way for ordering objects, for instance comparing different fields than are compared with Comparable; or your class does not implement Comparable or can not implement it because it is not yours, you must use other classes implementing Comparator
 
Trupti Samel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jose for ur instant reply.
Though I am still confused :-).They have mentioned in ApI that the ordering maintained by sorted map or set has to be consistent with the equals ,to correctly implement the map or set interface.
I know what consistent with equals means. But then to not getting the concept how it is actually working.
Could someone please help???
Trupti.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Set uses equals to determine if two objects are equivalent from its point of view. A TreeSet uses compareTo to order its elements and to compare them while accessing the TreeSet. If the natural order is consistent with equals both of these metods has to agree when considering two objects identical (for equals) and of the same order (for compareTo).
The same can be said for TreeMap
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic