Java Comparators and Comparable

I have only class files and there is no source code in java. Now I need to sort them, what should I use Comparator or Comparable?

Appreciate your help.

+4
source share
4 answers

Comparable is an interface implemented by classes that can compare themselves with another instance of this class.

Comparator is an interface for comparing two instances of another class.

If you have a Person class, for example

 public class Person implements Comparable<Person> { private final String firstName; private final String lastName; ... public int compareTo(Person that) { int rv = lastName.compareTo(that.lastName); if (rv == 0) rv = firstName.compareTo(that.firstName); return rv; } } 

This is a class that has a natural sort order by last name, and then by first name, i.e. Stephen Jones comes to John Smith.

If you wanted to sort these objects by first name and then last name so that John Smith came up with Stephen Jones, you would use the Comparator

 public class PersonComparator implements Comparator<Person> { public int compare(Person p1, Person p2) { int rv = p1.getFirstName().compareTo(p2.getFirstName()); if (rv == 0) rv = p1.getLastName().compareTo(p2.getLastName()); return rv; } } 

What you use depends on what control you have over classes and instances.

For example, if you have a class that does not implement Comparable , but you can subclass it to implement the interface, and you are sure that you will be the only person creating instances, then you can use Comparable .

In general, however, Comparator is more flexible.

+4
source

If you do not have access to the source file, use Comparator to sort them.

  class MyComparator implements Comparator<MyClass>{ @Override public int compare(MyClass o1, MyClass o2) { return o1.getType().compareTo(o2.getType()); } } // where getType is the getter method of the field on which you want to sort. 
0
source

If possible, you can extend the class and write a new class that implements Comparable so that you can implement compareTo (). Alternatively, you can use Comparator as indicated,

 class MyComparator implements Comparator<MyClass>{ @Override public int compare(MyClass o1, MyClass o2) { return o1.getType().compareTo(o2.getType()); } } 

Using a comparator provides additional configuration.

0
source

Simple answer

Go to Comparator .

Summary of two concepts

(extracted from http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html )

Comparable

A comparable object is able to compare itself with another object. The class itself must implement the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

The comparator object is able to compare two different objects. A class does not compare its instances, but some instances of other classes. This comparator class must implement the java.util.Comparator interface.

Consider an existing class

Your current class has already been implemented. Even if it already has a Comparable implementation, you don’t know how this comparison is implemented; therefore, you cannot predict your sorting. When you do not change the source of the class you are trying to sort, the only option is to go with a comparator.

0
source

Source: https://habr.com/ru/post/1434583/


All Articles