1

こんにちは、country クラスがあり、countryLanguage セットが含まれています。国ごとに言語を並べ替えたい。ビューページを作成しています。国とそれに対応する言語。郡は大丈夫ですが、その言語はソートされていません。どのクラスでコンパターを使用しているか、どのように使用しているか混乱しています。

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

複合 ID クラス

   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

keepのTreeSet代わりにa を使用できます:HashSetcountryLanguage

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

の要素はTreeSet、自然順序付けを使用して順序付けされるかComparator、通常はTreeSet作成時に提供される を使用して順序付けできます。

natural orderingofCountryLanguageを使いたい場合は、CountryLanguage実装しComparableます。

public class CountryLanguage implements Comparable<CountryLanguage>{

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

Comparatorまた、 a を使用して の要素を並べたい場合は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 に答える