我浏览了有关比较器和比较器如何工作的教程,但我对以下内容感到困惑:ComparatorMain 类调用
Collections.sort(listOfCountries,new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
return o1.getCountryName().compareTo(o2.getCountryName());
}
});
但是compareTo()方法的逻辑似乎是通过countryId而不是countryName进行比较:
package org.arpit.javapostsforlearning;
//If this.cuntryId < country.countryId:then compare method will return -1
//If this.countryId > country.countryId:then compare method will return 1
//If this.countryId==country.countryId:then compare method will return 0
public class Country implements Comparable{
int countryId;
String countryName;
public Country(int countryId, String countryName) {
super();
this.countryId = countryId;
this.countryName = countryName;
}
@Override
public int compareTo(Object arg0) {
Country country=(Country) arg0;
return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;
}
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
那么这怎么可能行得通呢?
这是整个 ComparatorMain 类:
package org.arpit.javapostsforlearning;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Country indiaCountry=new Country(1, 'India');
Country chinaCountry=new Country(4, 'China');
Country nepalCountry=new Country(3, 'Nepal');
Country bhutanCountry=new Country(2, 'Bhutan');
List<Country> listOfCountries = new ArrayList<Country>();
listOfCountries.add(indiaCountry);
listOfCountries.add(chinaCountry);
listOfCountries.add(nepalCountry);
listOfCountries.add(bhutanCountry);
System.out.println('Before Sort by id : ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country) listOfCountries.get(i);
System.out.println('Country Id: '+country.getCountryId()+'||'+'Country name: '+country.getCountryName());
}
Collections.sort(listOfCountries,new CountrySortByIdComparator());
System.out.println('After Sort by id: ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country) listOfCountries.get(i);
System.out.println('Country Id: '+country.getCountryId()+'|| '+'Country name: '+country.getCountryName());
}
//Sort by countryName
Collections.sort(listOfCountries,new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
return o1.getCountryName().compareTo(o2.getCountryName());
}
});
System.out.println('After Sort by name: ');
for (int i = 0; i < listOfCountries.size(); i++) {
Country country=(Country) listOfCountries.get(i);
System.out.println('Country Id: '+country.getCountryId()+'|| '+'Country name: '+country.getCountryName());
}
}
}
教程: http://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html
最佳答案
如果您使用
Collections.sort(countries);
然后,这些国家/地区将使用其自然顺序进行排序,即由其 compareTo()
方法定义的顺序。因此,它们将按 ID 排序。仅当集合包含 Comparable 对象的实例时,您才能以这种方式对集合进行排序。
如果您使用
Collections.sort(countries, someComparator);
然后,将使用作为参数传递的比较器 (someComparator) 定义的顺序对国家/地区进行排序。在您的情况下,由于比较器按名称比较国家/地区,因此它们将按名称排序。您可以通过这种方式对任何类型的对象进行排序。
因此,简而言之,传递比较器可以按照与类本身定义的顺序不同的顺序对事物进行排序。
关于java - 比较器和比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23320805/