2014-04-27 98 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