user173683
user173683

Reputation:

where need to use collections.sort() and comparable and comparator interfaces in java collection?

I got some understanding of collections which gone through some articles.
But i 'm confusing where should implement collections.sort() method and where need to use comparable interface(compareTo() and comparator interface (compare()).

Comparable interface for compare this and another reference object but comparator for compare two objects.

I would like to know exactly which situation need to use methods ?

Thanks,

Upvotes: 2

Views: 5950

Answers (4)

user3155808
user3155808

Reputation: 1

I guess Comparator is used only when you want to sort the objects in a class in the ascending order. Used to sort say the ID's of a class in ascending order. It is mutually exclusive sorting based on one field automatically rules out sorting based on another field

Upvotes: 0

Barranka
Barranka

Reputation: 21047

Let's see if I can explain it in plain words:

The compareTo() method is design to compare the active instance of an object with another instance of an object. So, let's say you have this example class:

public class Spam implements Comparable<Spam> {
    private int eggs;
    public Spam(int eggs) {
        this.eggs = eggs;
    }
    public int compareTo(Spam otherSpam) {
        return otherSpam.eggs - this.eggs;
    }
    public int getEggs() {
        return eggs;
    }
}

Let's say you use this Spam class somewhere in your code:

...
if(spam1.compareTo(spam2) == 0) {
    System.out.println("Both spams are equal");
} else {
    if(spam1.compareTo(spam2) > 0)
        System.out.println("'spam2' is bigger than 'spam1'");
    else
        System.out.println("'spam2' is smaller than 'spam1'");
}
....

So, compareTo() is used to decide wether two instance of a class are equal, and, if they are not equal, which one is bigger.

The importance of this for a sorting algorithm is evident now. How would you sort a collection of objects if you don't know if one is bigger than another? When an implementation of sort is called, it uses the compareTo() method of the class to decide the correct order of the collection.

Finally, the compare() method is something you use when you want to perform the comparison outside the class. An implementation of this would be:

 public int compare(Spam s1, Spam s2) {
     return s1.getEggs() - s2.getEggs();
 }

Hope this helps you

Upvotes: 0

rgettman
rgettman

Reputation: 178293

You should not implement Collections.sort(); this method is built-in to Java. Call that method without supplying a Comparator to sort by the natural order, if it's Comparable. Else, supply a Comparator to sort the Comparator's way.

You should have the class implement Comparable and provide a compareTo method if the class has a natural ordering, as indicated in the Comparable javadocs. An example would be for Integer and Double, which certainly have a natural mathematical ordering.

You should create a class that implements Comparator when you cannot make the class of the object to sort Comparable or when you want to present an ordering that is an alternative to the natural ordering, or when you want to impose an order when there is no natural ordering. An example would be to reverse the natural order (say, sort descending, from largest to smallest). Another example would be a data object with multiple fields, that you want to be sortable on multiple fields, e.g. a Person object with firstName and lastName: you could have a Comparator that sorts on lastName first, and another that sorts on firstName first.

Upvotes: 2

ssantos
ssantos

Reputation: 16536

Comparator makes sense to me when I don't have access to the code of the class I whish to compare: for instance, you may need to implement a custom comparator for String.

When I need to sort lists of my own custom objects, I write them to implement interface Comparable.

Upvotes: 0

Related Questions