简体   繁体   中英

sort object using comparable for arraylist of objects

Hi I have a country class and that contains a countryLanguage set. I want to sort the language for each country. I am createing view page. country and his corresponding language. county is ok but its language are not in sorted order. I am confused in which class i have use compartor and how.

public class Country implements Serializable{
private Set<CountryLanguage> countryLanguage = new HashSet<CountryLanguage>(0);
//...
}
public class CountryLanguage {
private CountryLanguageID countryLangPK = new CountryLanguageID();
//...
}

composit id class

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

You can use a TreeSet instead of HashSet to keep countryLanguage :

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

The elements of TreeSet are ordered using their natural ordering or can be ordered by using a Comparator , typically provided at TreeSet creation time.

If you want to go with natural ordering of CountryLanguage , make CountryLanguage implement Comparable :

public class CountryLanguage implements Comparable<CountryLanguage>{

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

And if you want to use a Comparator to order elements of countryLanguage , define a comparator:

private static final Comparator<CountryLanguage> COMP = new Comparator<CountryLanguage>() {

        @Override
        public int compare(CountryLanguage o1, CountryLanguage o2) {
            // Compare o1 with o2
        }
};

and use it while creating your TreeSet :

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

EDIT:

Your set is of type CountryLanguage . So in order to sort the elements of Set<CountryLanguage> countryLanguage , you need to make CountryLanguage implement Comparable (but you have defined CountryLanguageID to implement Comparable ):

And while comparing instances of CountryLanguage , you can use CountryLanguageID property for comparison:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM