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.
I need some help understanding Comparable interface
java - adding comparable interface and adding a compareTo() method - Stack Overflow
Trying to understand the point of Comparable (if I even understand...)
Comparable interface and comparing String length in Java; CompSci student having difficulty, but almost there
Videos
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.
You need to:
- Add
implements Comparable<Animal>to the class declaration; and - Implement a
int compareTo( Animal a )method to perform the comparisons.
Like this:
public class Animal implements Comparable<Animal>{
public String name;
public int year_discovered;
public String population;
public Animal(String name, int year_discovered, String population){
this.name = name;
this.year_discovered = year_discovered;
this.population = population;
}
public String toString(){
String s = "Animal name: "+ name+"\nYear Discovered: "+year_discovered+"\nPopulation: "+population;
return s;
}
@Override
public int compareTo( final Animal o) {
return Integer.compare(this.year_discovered, o.year_discovered);
}
}
I'm struggling to understand why we need to use compareTo for comparing things when I can just create a method that does the same thing. What I understand so far is that Interfaces can hold abstract methods without implementation, and once you implement that interface on a class you have to override the method from the interface and write the implementation. What I don't understand is what is significant about (implements Comparable<T>) if all I'm gonna do is override compareTo method so it returns 1 if larger, -1 if smaller or 0 if equal.
If you have somekind of List<Employee> the you can use the static method Collections.sort() to sort it without implementing the Comparable interface on the Employee class. You can simply use the variant of the sort function that accepts an Comparator object which will be used to compare two instances of the Employee class.
And you don't even need to implement one on your own. You can simply use Comparator.comparingInt(e -> e.numOfSales) to create the correct Comparator to sort your list.
So
Collections.sort(employees, Comparator.comparingInt(e -> e.numOfSales));
would be sufficent to sort your employee list.
An Employee should be comparable to other Employees, so it's Comparable<Employee>. You then need to implement the compareTo method, as the error message says:
public class Employee implements Comparable<Employee> {
private int numOfSales;
// other data members
// Constructors, getters, setters, etc
@Override
public int compareTo(Employee e) {
return Integer.compare(numOfSales, e.numOfSales);
}
}
What I understand is that it is an interface. An interface is a guarantee that we will implement the methods it provides somehow. The one method provided by Comparable is compareTo() that returns an int (like -1 if the second obj is greater, 0 if =, 1 if first obj is greater).
My questions is that was is the point of implements Comparable if we write our own version of the method to compare what we want anyways? Am I missing something about implementing and interfaces??