java - 使用可比较的对象数组列表对对象进行排序

标签 java sorting collections

您好,我有一个国家/地区类,其中包含一个国家/地区语言集。 我想对每个国家/地区的语言进行排序。 我正在创建 View 页面。国家及其对应的语言。 县还可以,但其语言未按顺序排列。 我很困惑我在哪个类中使用了 compartor 以及如何使用。

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());
}
}

最佳答案

您可以使用 TreeSet而不是HashSet保持countryLanguage :

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

TreeSet的元素使用其自然顺序进行排序,或者可以使用 Comparator 进行排序,通常在 TreeSet 处提供创建时间。

如果您想使用natural orderingCountryLanguage ,使CountryLanguage实现Comparable :

public class CountryLanguage implements Comparable<CountryLanguage>{

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

如果你想使用ComparatorcountryLanguage 的元素进行排序,定义一个比较器:

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());
}
...
}

关于java - 使用可比较的对象数组列表对对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316217/

相关文章:

java - 检索 ArrayListMultimap 键

java - 日期解析 yyyy-MM-dd'T'HH :mm:ss in Android

javax.el.PropertyNotFoundException : Property 'Order' not found on type mine. 域.OrderHistory

java - 使用 Criteria 运行子查询时出现 ClassCastException (String to Long)

c - 如何编写单个函数以按行和按列排序的二维数组进行搜索(限制是函数应该类似于 int search(int *A,int n,int key))

c++ - 寻找更好的数据排序方法

java - 如何在 Java 中按字母顺序组织对象数组

java - 使用 JUnit 进行单例测试

java - 当我编写以下代码时,为什么我的输出是 "4 10 7"而不是 "4 3 10 7"

java - 使用 Class 对象实例化 Java 类型参数/泛型