2013-10-11 2 views
1

안녕하세요 저는 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()); 
} 
} 

답변

2

당신은 countryLanguage을 유지하기 위해 TreeSet 대신 HashSet의를 사용할 수 있습니다 TreeSet

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

요소는 자연 순서로 주문하거나 주문하실 수 있습니다 Comparator을 사용하여 일반적으로 TreeSet 작성 시간에 제공됩니다.

public class CountryLanguage implements Comparable<CountryLanguage>{ 

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

을 그리고 당신은 countryLanguage의 요소를 주문하는 Comparator을 사용하려는 경우, 비교기 정의 :

당신이 CountryLanguagenatural ordering로 갈 수 있도록하려는 경우 CountryLanguageComparable 구현

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의 요소를 정렬하려면, 당신은 CountryLanguageComparable을 구현해야 (하지만 당신은 Comparable을 구현하기 위해 CountryLanguageID을 정의) :

그리고 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()); 
} 
... 
} 
+0

해당 기능이 작동하지 않습니다. 나는 두 가지 논리를 모두 시도했다. 첫 번째 논리에서는 this.getCountryLangPK(). getLanguage(). getLanguageName(). compareTo (o2.getCountryLangPK(). getLanguage(). getLanguageName())와 같이 countryLanguage 클래스에 비교 로그인을 작성했습니다. 더 이상 괴롭히지 않을 경우 – Surya

+0

새 코드를 포함하도록 질문을 편집 할 수 있습니까? –

+0

수정 된 코드 .... composits key – Surya

관련 문제