Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the Java comparable interface?

I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it:

public class Animal{     public String name;     public int yearDiscovered;     public String population;      public Animal(String name, int yearDiscovered, String population){         this.name = name;         this.yearDiscovered = yearDiscovered;         this.population = population; }      public String toString(){         String s = "Animal name: "+ name+"\nYear Discovered: "+yearDiscovered+"\nPopulation: "+population;         return s;     } } 

I have a test class that will create objects of type Animal however I want to have a comparable interface inside this class so that older years of discovery rank higher than low. I have no idea on how to go about this though.

like image 790
Softey Avatar asked Feb 07 '14 11:02

Softey


People also ask

Can we implement comparable interface?

We can implement the Comparable interface with the Movie class, and we override the method compareTo() of Comparable interface.

How do you implement an interface in Java?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).

Why would you implement a comparable interface for your class what's it's used for?

Comparable interface is mainly used to sort the arrays (or lists) of custom objects. Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.

What does implement comparable mean in Java?

The Java Comparable interface, java. lang. Comparable , represents an object which can be compared to other objects. For instance, numbers can be compared, strings can be compared using alphabetical comparison etc. Several of the built-in classes in Java implements the Java Comparable interface.


2 Answers

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.

like image 119
user1983983 Avatar answered Sep 20 '22 06:09

user1983983


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);     } } 
like image 44
MT0 Avatar answered Sep 18 '22 06:09

MT0