안녕하세요 저는 country 클래스가 있고 countryLanguage 세트가 들어 있습니다. 각 국가의 언어를 정렬하고 싶습니다. 보기 페이지를 생성 중입니다. 국가 및 해당 언어 카운티는 괜찮지 만 해당 언어는 정렬 된 순서가 아닙니다. 어떤 클래스에서 사용 compartor 및 방법을 혼란 스러워요. 객체의 arraylist에 대해 비교할 수있는 객체 정렬
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());
}
}
해당 기능이 작동하지 않습니다. 나는 두 가지 논리를 모두 시도했다. 첫 번째 논리에서는 this.getCountryLangPK(). getLanguage(). getLanguageName(). compareTo (o2.getCountryLangPK(). getLanguage(). getLanguageName())와 같이 countryLanguage 클래스에 비교 로그인을 작성했습니다. 더 이상 괴롭히지 않을 경우 – Surya
새 코드를 포함하도록 질문을 편집 할 수 있습니까? –
수정 된 코드 .... composits key – Surya