[英]How to implement the Java comparable interface?
我不確定如何在我的抽象類中實現類似的接口。 我有以下示例代碼,我正在使用它來嘗試理解它:
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;
}
}
我有一個測試類,它將創建動物類型的對象,但是我希望在這個類中有一個可比較的接口,以便較早的發現排名高於低。 我不知道該怎么做。
您只需定義Animal implements Comparable<Animal>
即public class Animal implements Comparable<Animal>
。 然后你必須以你喜歡的方式實現compareTo(Animal other)
方法。
@Override
public int compareTo(Animal other) {
return Integer.compare(this.year_discovered, other.year_discovered);
}
使用compareTo
的這種實現,具有更高year_discovered
的動物將獲得更高的排序。 我希望您通過這個示例了解Comparable
和compareTo
的概念。
你需要:
implements Comparable<Animal>
; 和int compareTo( Animal a )
方法來執行比較。像這樣:
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);
}
}
當您在其中時,我建議記住有關 compareTo() 方法的一些關鍵事實
CompareTo 必須與 equals 方法一致,例如,如果兩個對象通過 equals() 相等,則 compareTo() 必須返回零,否則如果這些對象存儲在 SortedSet 或 SortedMap 中,它們將無法正常運行。
如果當前對象與 null 對象進行比較,CompareTo() 必須拋出 NullPointerException,而不是在這種情況下返回 false 的 equals()。
閱讀更多:http: //javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html#ixzz4B4EMGha3
在您的類中實現Comparable<Animal>
接口並在您的類中提供int compareTo(Animal other)
方法的實現。 看到這篇文章
您需要實現接口並定義 compareTo() 方法。 要獲得好的教程,請訪問 -教程點鏈接或MyKongLink
Emp 類需要實現 Comaparable 接口,所以我們需要重寫它的 compateTo 方法。
import java.util.ArrayList;
import java.util.Collections;
class Emp implements Comparable< Emp >{
int empid;
String name;
Emp(int empid,String name){
this.empid = empid;
this.name = name;
}
@Override
public String toString(){
return empid+" "+name;
}
@Override
public int compareTo(Emp o) {
if(this.empid==o.empid){
return 0;
}
else if(this.empid < o.empid){
return 1;
}
else{
return -1;
}
}
}
public class JavaApplication1 {
public static void main(String[] args) {
ArrayList<Emp> a= new ArrayList<Emp>();
a.add(new Emp(10,"Mahadev"));
a.add(new Emp(50,"Ashish"));
a.add(new Emp(40,"Amit"));
Collections.sort(a);
for(Emp id:a){
System.out.println(id);
}
}
}
需要API Version 19
的Integer.compare
方法的源代碼的可能替代方法是:
public int compareTo(Animal other) { return Integer.valueOf(this.year_discovered).compareTo(other.year_discovered); }
此替代方法不需要您使用API version 19
。
這是實現java可比接口的代碼,
// adding implements comparable to class declaration
public class Animal implements Comparable<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;
}
@Override
public int compareTo(Animal other) // compareTo method performs the comparisons
{
return Integer.compare(this.year_discovered, other.year_discovered);
}
}
使用比較器...
public class AnimalAgeComparator implements Comparator<Animal> {
@Override
public int compare(Animal a1, Animal a2) {
...
}
}
這是另一個例子
public class Book implements Comparable<Book>{
int id;
String name="";
double price;
public Book(int id, String name, double price) {
this.id = id;
setName(name);
setPrice(price);
}
public void setName(String name) {
this.name = "";
this.name += name;
}
public void setPrice(double price) {
this.price = price;
}
/**
* return this as a String in the required format
*/
@Override
public String toString() {
String result = String.format("%5d %-45s %10.2f", this.id, this.name, this.price);
return result;
}
@Override
public int compareTo(Book compareBook) {
Double comparePrice = ((Book)compareBook).price;
return (int)(comparePrice-this.price);
}
}
這件事可以通過實現一個實現 Comparable 的公共類來輕松完成。 這將允許您使用 compareTo 方法,該方法可與您希望比較的任何其他對象一起使用。
例如,您可以通過以下方式實現它:
public String compareTo(Animal oth)
{
return String.compare(this.population, oth.population);
}
我認為這可能會解決您的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.