嗨我有一个国家类,并包含一个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());
}
}
它不工作。我已经尝试了两种逻辑。在第一个逻辑中,我在countryLanguage类中编写了比较登录,如this.getCountryLangPK()。getLanguage()。getLanguageName()。compareTo(o2.getCountryLangPK()。getLanguage()。getLanguageName());你可以编辑你的问题以包含新的代码,任何更多的暗示都可以通过 – Surya
了解。 –
编辑的代码....编辑我的答案编辑键 – Surya