Why Should a Java Class Implement Comparable

Why should a Java class implement comparable?

Here is a real life sample. Note that String also implements Comparable.

class Author implements Comparable<Author>{
String firstName;
String lastName;

@Override
public int compareTo(Author other){
// compareTo should return < 0 if this is supposed to be
// less than other, > 0 if this is supposed to be greater than
// other and 0 if they are supposed to be equal
int last = this.lastName.compareTo(other.lastName);
return last == 0 ? this.firstName.compareTo(other.firstName) : last;
}
}

later..

/**
* List the authors. Sort them by name so it will look good.
*/
public List<Author> listAuthors(){
List<Author> authors = readAuthorsFromFileOrSomething();
Collections.sort(authors);
return authors;
}

/**
* List unique authors. Sort them by name so it will look good.
*/
public SortedSet<Author> listUniqueAuthors(){
List<Author> authors = readAuthorsFromFileOrSomething();
return new TreeSet<Author>(authors);
}

Why implement Comparable interface when you can define compareTo method in a class?

The benefit of implementing the interface is that some methods specifically require object that implements the Comparable interface. It gives them a guarantee that the object you're passing has a compareTo method with the correct signature.

There's no way in Java to have a method require that an object implement any given method (such as compareTo) in and of itself. To get around this, interfaces were created. Any time you have an object that you know is a Comparable, you also know you can call compareTo on it.

How to implement the Java comparable interface?

You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it.

@Override
public int compareTo(Animal other) {
return Integer.compare(this.year_discovered, other.year_discovered);
}

Using this implementation of compareTo, animals with a higher year_discovered will get ordered higher. I hope you get the idea of Comparable and compareTo with this example.

Why use the comparable interface in Java?

Comparable is an interface, hence it imposes a contract that others may follow. For example, calling Collections.sort(list) only works if the elements are instances of Comparable, and internally relies on the compareTo method to implement the sorting.

When to use Comparable and Comparator

I would say that an object should implement Comparable if that is the clear natural way to sort the class, and anyone would need to sort the class would generally want to do it that way.

If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then a Comparator is a better option.

Put another way, given the class name, is it clear how a comparable would sort, or do you have to resort to reading the javadoc? If it is the latter, odds are every future sorting use case would require a comparator, at which point the implementation of comparable may slow down users of the class, not speed them up.

when to implement comparable and when to implement equals in Java

If you only ever need to compare them for equality (or put them in a HashMap or HashSet which is effectively the same) you only need to implement equals and hashcode.

If your objects have an implicit order and you indend to sort them (or put them in a TreeMap or TreeSet which is effectively sorting) then you must implement Comparable or provide a Comparator.

What is point of implementing the Comparable interface in my classes?

The Comparable interface provides a means of communication to the implemented sorting algorithms, which would be impossible using custom methods for comparison.

when to use extends or implements Comparable (Java) ? + why I cannot create object

T which is your type Item needs to implement Comparable. This will allow the ExpandableArrayList class to run the compareTo method on elements of type Item using the comparison provided by that class. When you implement compareTo from Comparable you have to give a way for comparing different Items, this should be based on the attributes of the Item class ideally.

public class Item implements Comparable<Item> {... }

This is what the class def would look like.

You have to write implements for interfaces and extends for classes.

Inheritance tutorial

Interfaces tutorial



Related Topics



Leave a reply



Submit