2013-10-11 68 views
1

嗨我有一个国家类,并包含一个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()); 
} 
} 

回答

2

可以使用的TreeSet代替HashSet保持countryLanguage

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

TreeSet的元素使用其自然顺序进行排序或可订购通过使用通常在TreeSet创建时间提供的Comparator

如果你想要去的CountryLanguagenatural ordering,使CountryLanguage实施Comparable

public class CountryLanguage implements Comparable<CountryLanguage>{ 

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

如果你想使用Comparator订购的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); 

编辑:

setCountryLanguage类型。因此,为了给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()); 
} 
... 
} 
+0

它不工作。我已经尝试了两种逻辑。在第一个逻辑中,我在countryLanguage类中编写了比较登录,如this.getCountryLangPK()。getLanguage()。getLanguageName()。compareTo(o2.getCountryLangPK()。getLanguage()。getLanguageName());你可以编辑你的问题以包含新的代码,任何更多的暗示都可以通过 – Surya

+0

了解。 –

+0

编辑的代码....编辑我的答案编辑键 – Surya