2015-09-03 84 views
0

我有一個具有幾個變量的Person類。 City類具有Person對象列表作爲其唯一變量。而班級國家有城市對象列表作爲其唯一變量。排序在java中有列表的對象的列表

從測試類我創建了一個包含城市對象的數組列表。如何排列列表中的City對象?即如何對包含一類對象的數組列表進行排序,其中每個對象都有另一個列表。

Person類

public class Person { 

     private String name; 
     private Integer age; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public Integer getAge() { 
      return age; 
     } 

     public void setAge(Integer age) { 
      this.age = age; 
     } 

     public static Comparator<Person> personComparator = new Comparator<Person>() { 
      @Override 
      public int compare(Person o1, Person o2) { 
       int returnResult = o1.getName().compareTo(o2.getName()); 
       if (returnResult == 0) { 
        return o1.getAge().compareTo(o2.getAge()); 
       } 
       return returnResult; 
      } 
     }; 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((age == null) ? 0 : age.hashCode()); 
      result = prime * result + ((name == null) ? 0 : name.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (!(obj instanceof Person)) 
       return false; 
      Person other = (Person) obj; 
      if (age == null) { 
       if (other.age != null) 
        return false; 
      } else if (!age.equals(other.age)) 
       return false; 
      if (name == null) { 
       if (other.name != null) 
        return false; 
      } else if (!name.equals(other.name)) 
       return false; 
      return true; 
     } 

    } 

城市類:

 public class City { 
     private List<Person> personList; 

     public List<Person> getPersonList() { 
      if (personList == null) { 
       personList = new ArrayList<Person>(); 
      } 
      return personList; 
     } 

     public void setPersonList(List<Person> personList) { 
      this.personList = personList; 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result 
        + ((personList == null) ? 0 : personList.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (!(obj instanceof City)) 
       return false; 
      City other = (City) obj; 
      if (personList == null) { 
       if (other.personList != null) 
        return false; 
      } else if (!personList.equals(other.personList)) 
       return false; 
      return true; 
     } 

    } 

State類:

public class State { 
    private List<City> cityList; 

    public List<City> getCityList() { 
     if (cityList == null) { 
      cityList = new ArrayList<City>(); 
     } 
     return cityList; 
    } 

    public void setCityList(List<City> cityList) { 
     this.cityList = cityList; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result 
       + ((cityList == null) ? 0 : cityList.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (!(obj instanceof State)) 
      return false; 
     State other = (State) obj; 
     if (cityList == null) { 
      if (other.cityList != null) 
       return false; 
     } else if (!cityList.equals(other.cityList)) 
      return false; 
     return true; 
    } 

} 

public class TestClass { 

    public static void main(String[] args) { 
     Person p1 = new Person(); 
     p1.setName("John"); 
     p1.setAge(40); 

     Person p2 = new Person(); 
     p2.setName("Antony"); 
     p2.setAge(50); 

     Person p3 = new Person(); 
     p3.setName("Bob"); 
     p3.setAge(24); 

     Person p4 = new Person(); 
     p4.setName("Mark"); 
     p4.setAge(35); 

     City city1 = new City(); 
     city1.getPersonList().add(p1); 
     city1.getPersonList().add(p2); 

     City city2 = new City(); 
     city2.getPersonList().add(p3); 
     city2.getPersonList().add(p4); 

     State state1 = new State(); 
     state1.getCityList().add(city1); 
     state1.getCityList().add(city2); 

     //How to sort the citylist in the state object? 



    } 

} 
+0

你想根據什麼排序清單?人數? –

+0

基本上我需要檢查兩個狀態對象是否相等。狀態對象具有城市列表。所以,我需要首先對城市進行排序以完成arraylist.equals(anotherArrayList)。問題是,arraylist.equals(anotherarraylist)正在根據訂單進行檢查。所以,我需要在狀態對象中首先排序(排序)城市。 – Karthick

回答

0

開發一種被稱爲compareTo方法,全市對象中比較了現場,以確定的順序你可以調用Collections.sort()方法。在初始化的數組列表對象上。查看here瞭解更多信息。

+0

我想到了這一點,但是如果你看到除了Person列表之外,City對象內沒有字段。 – Karthick

0

您可以查看Comparable接口。 Here是文檔。