wood burning stoves*
The moose likes Beginning Java and the fly likes Comparator and Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparator and Comparable" Watch "Comparator and Comparable" New topic

Comparator and Comparable

Hyndavi Padamata

Joined: Apr 19, 2007
Posts: 12
Can any one explain comparator and comparable interfaces in collections clearly?

[Edit to provide meaningful topic - Dave]
[ April 25, 2007: Message edited by: David O'Meara ]
prashant fusate

Joined: Aug 24, 2005
Posts: 16
Comparable interface shows that the class knows how to compare ITSELF
against another class.

Comparator interface allows a single class know how to compare two
classes which (typically) are not the same type as the comparator.

Its about moving the responsibility for doing the comparison from the
class that is being compared, to another class who's sole responsibility
is to be the comparator.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Please, UseAMeaningfulSubjectLine.

The JavaDocs do this, backed up by the Collections framework tutorial. Rather than rehash what is written there can you explain what it is you don't understand? Are you wondering why there are two simmilar interfaces? Or how to use them?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rambabu Gonela

Joined: Jul 31, 2006
Posts: 24
In the real business world, there are many cases where we're required to perform sorting on a Collection of some user-defined objects. If the element of the Collection is a String type, or any other class which implements Comparable, then the sorting can be performed in the natural order. But sometimes, we want to perform a different criteria for sorting, or probably we want to sort a user-defined type.

Here's where Comparator comes into play. Clean and extensible.

Here's an example of a simplified user-defined type:

In the above class, the most commonly found as natural ordering would be order by lastName. This can easily be implemented using the Comparable interface, i.e.

Sorting a List of Customer objects would be as simple as:

Collections.sort (customerList);

But, if we want to use a different ordering, e.g. order by the first name, then we cannot use the natural ordering as defined within the Customer class. Instead, we have to define an alternative ordering, in the form of a Comparator class.

Sorting a List of Customer objects by their first name, would be:

// assuming you implement singleton..
Comparator comparator =

Collections.sort (customerList, comparator);

Simple, clean & extensible. You can start defining more and more Comparator classes to suit your needs. As it is a Java class, you can also perform complex comparison on the objects.
I agree. Here's the link: http://aspose.com/file-tools
subject: Comparator and Comparable