1

嗨,我有一个国家类,其中包含一个国家语言集。我想对每个国家/地区的语言进行排序。我正在创建视图页面。国家及其对应的语言。县还可以,但它的语言没有按顺序排列。我很困惑我在哪个班级使用了比较器以及如何使用。

public class Country implements Serializable{
private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);
//...
}
public class CountryLanguage {
private CountryLanguageID countryLangPK = new CountryLanguageID();
//...
}

复合标识类

   public class CountryLanguageID implements Serializable,Comparable<CountryLanguageID>{

private Country country;
private Language language;

@ManyToOne(fetch=FetchType.LAZY)
public Country getCountry() {
    return country;
}
public void setCountry(Country country) {
    this.country = country;
}

@ManyToOne(fetch=FetchType.LAZY)
public Language getLanguage() {
    return language;
}
public void setLanguage(Language language) {
    this.language = language;
}

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    CountryLanguageID that = (CountryLanguageID) o;

    if (country != null ? !country.equals(that.country) : that.country != null){
        return false;
    }
    if (language != null ? !language.equals(that.language) : that.language != null){
        return false;
    }
    return true;
}
public int hashCode() {
    int result;
    result = (country != null ? country.hashCode() : 0);
    result = 31 * result + (language != null ? language.hashCode() : 0);
    return result;
}
@Override
public int compareTo(CountryLanguageID o) {

    //return this.language.compareTo(o.language);
    return this.getLanguage().getLanguageName().compareTo(o.getLanguage().getLanguageName());
}
}
4

1 回答 1

2

您可以使用 aTreeSet而不是HashSet保留countryLanguage

private Set<CountryLanguage> countryLanguage = new TreeSet<CountryLanguage>(0);

的元素TreeSet使用其自然顺序进行排序,或者可以使用Comparator通常在TreeSet创建时提供的 进行排序。

如果您想使用natural orderingof CountryLanguage,请CountryLanguage执行以下操作Comparable

public class CountryLanguage implements Comparable<CountryLanguage>{

@Override
public int compareTo(CountryLanguage cl) {
    // Comparison logic
}
...
}

如果您想使用 aComparator来订购 的元素countryLanguage,请定义一个比较器:

private static final Comparator<CountryLanguage> COMP = new Comparator<CountryLanguage>() {

        @Override
        public int compare(CountryLanguage o1, CountryLanguage o2) {
            // Compare o1 with o2
        }
};

并在创建您的时使用它TreeSet

 private Set<CountryLanguage> countryLanguage = new TreeSet<CountryLanguage>(COMP);

编辑:

set的类型是CountryLanguage. 因此,为了对 的元素进行排序Set<CountryLanguage> countryLanguage,您需要CountryLanguage实现Comparable(但您已定义CountryLanguageID为实现Comparable):

在比较 的实例时CountryLanguage,您可以使用CountryLanguageID属性进行比较:

public class CountryLanguage implements Comparable<CountryLanguage>{
private CountryLanguageID countryLangPK = new CountryLanguageID();
...    
@Override
public int compareTo(CountryLanguage cl) {
return this.countryLangPK.getLanguage().getLanguageName().compareTo(cl.getCountryLangPK().getLanguage().getLanguageName());
}
...
}
于 2013-10-11T10:54:55.540 回答