2014-04-27 102 views
0

我通过教程看如何比较和可比的作品,但我难倒了以下内容:ComparatorMain类调用比较器和可比

Collections.sort(listOfCountries,new Comparator<Country>() { 

        @Override 
        public int compare(Country o1, Country o2) { 
         return o1.getCountryName().compareTo(o2.getCountryName()); 
        } 
       }); 

但的compareTo()的逻辑方法似乎是在做一个比较countryId,not 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

+0

我不明白你的问题。你能否重新解释你的问题? – bitli

+0

comparatorMain类有一个Collections.sort(),它带有一个带有compare()方法的匿名类。这个compare()方法调用并返回来自o1.getCountryName()的一个compareTo()方法的结果,它只是一个String。 compareTo()必须在类中实现,所以String如何使用它?唯一实现的compareTo()方法比较countryID不是countryName。我不了解什么? – user2778481

回答

3

如果使用

Collections.sort(countries); 

那么国家将利用自己的自然顺序进行排序,即排序由其compareTo()方法定义。他们将因此按ID排序。如果集合包含Comparable对象的实例,则只能按此方式对集合进行排序。

如果使用

Collections.sort(countries, someComparator); 

那么国家将使用由比较器(someComparator)中定义的顺序排序传入作为参数。在你的情况下,由于比较器按名称比较国家,它们将按名称排序。你可以用这种方法排序任何类型的对象。

因此,简而言之,传递一个比较器就是允许按照不同于该类本身定义的顺序来排序的东西。

+0

但compare()方法返回o1.getCountryName()。compareTo(o2.getCountryName());所以我们的第一个对象o1的String countryName将调用它的compareTo方法传递第二个对象o2的countryName作为比较。我不理解的是,当compareTo()必须在类中实现时,我们如何在字符串上执行compareTo()。其次,如果它以某种方式工作,我们将传递该对象,但Country类中的compareTo()方法会对countryID进行比较,而不是countryName。 – user2778481

+0

String还实现了Comparable,因此可以在String上调用compareTo,将它与另一个String进行比较。 –

+0

好吧,它已经在String对象中实现了,你说的是什么? – user2778481