Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting java中包含列表的对象的排序列表_Sorting_Arraylist - Fatal编程技术网

Sorting java中包含列表的对象的排序列表

Sorting java中包含列表的对象的排序列表,sorting,arraylist,Sorting,Arraylist,我有一个类Person,它有两个变量。类City的唯一变量是Person对象列表。类State的唯一变量是城市对象列表 从一个测试类中,我创建了一个包含城市对象的arraylist。如何对列表中的城市对象进行排序?i、 例如,如何对包含一类对象的arraylist进行排序,其中每个对象都有另一个列表 人类 public class Person { private String name; private Integer age; pu

我有一个类Person,它有两个变量。类City的唯一变量是Person对象列表。类State的唯一变量是城市对象列表

从一个测试类中,我创建了一个包含城市对象的arraylist。如何对列表中的城市对象进行排序?i、 例如,如何对包含一类对象的arraylist进行排序,其中每个对象都有另一个列表

人类

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

    }
公共类人物{
私有字符串名称;
私人整数年龄;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数期限){
这个。年龄=年龄;
}
公共静态比较器personComparator=新比较器(){
@凌驾
公共整数比较(o1人、o2人){
int returnResult=o1.getName().compareTo(o2.getName());
if(returnResult==0){
返回o1.getAge().compareTo(o2.getAge());
}
返回结果;
}
};
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((age==null)?0:age.hashCode();
result=prime*result+((name==null)?0:name.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(!(人的obj实例))
返回false;
人员其他=(人员)obj;
如果(年龄==null){
if(other.age!=null)
返回false;
}如果(!age.equals(other.age))
返回false;
if(name==null){
if(other.name!=null)
返回false;
}如果(!name.equals(other.name))
返回false;
返回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;
        }

    }
公共级城市{
私人名单个人名单;
公共列表getPersonList(){
if(personList==null){
personList=新的ArrayList();
}
回归人格;
}
public void setPersonList(列表personList){
this.personList=personList;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果
+((personList==null)?0:personList.hashCode());
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(!(城市的obj实例))
返回false;
城市其他=(城市)obj;
if(personList==null){
if(other.personList!=null)
返回false;
}如果(!personList.equals(other.personList))为else
返回false;
返回true;
}
}
国家级:

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?



    }

}
公共类状态{
私人名单城市名单;
公共列表getCityList(){
if(cityList==null){
cityList=newarraylist();
}
返回城市列表;
}
public void setCityList(列表cityList){
this.cityList=城市列表;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果
+((cityList==null)?0:cityList.hashCode());
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(!(obj实例状态))
返回false;
州其他=(州)obj;
if(cityList==null){
if(other.cityList!=null)
返回false;
}如果(!cityList.equals(other.cityList))
返回false;
返回true;
}
}
公共类TestClass{
公共静态void main(字符串[]args){
人员p1=新人员();
p1.设定名称(“约翰”);
p1.设置(40);
人员p2=新人员();
p2.设定名称(“安东尼”);
p2.设定值(50);
人员p3=新人员();
p3.设定名称(“Bob”);
p3.设置(24);
人员p4=新人员();
p4.设置名称(“标记”);
p4.设置(35);
城市1=新城();
city1.getPersonList().add(p1);
city1.getPersonList().add(p2);
城市2=新城();
city2.getPersonList().add(p3);
city2.getPersonList().add(p4);
state1=新状态();
state1.getCityList().add(city1);
state1.getCityList().add(city2);
//如何在state对象中对citylist进行排序?
}
}

开发一个名为compareTo的方法,用于比较city对象中的某些字段以确定所需的顺序,完成此操作后,可以调用Collections.sort()方法;在初始化的数组列表对象上。查看了解更多信息。

您可以查看类似的界面。是文档

您想根据什么对列表进行排序?人数?基本上我需要检查两个状态对象是否相等。State对象具有城市列表。因此,我需要首先对城市进行排序,以执行arraylist.equals(另一个arraylist)。问题是arraylist.equals(另一个arraylist)正在根据顺序进行检查。所以,我需要先在state对象中对城市进行排序。我想到了,但是如果你