排序在java中具有列表的对象列表

时间:2015-09-03 19:34:38

标签: sorting arraylist

我有一个Person类,它有几个变量。 City类具有Person对象列表作为其唯一变量。类State将城市对象列表作为唯一变量。

在测试类中,我创建了一个包含City对象的arraylist。如何对列表中的City对象进行排序?即,如何对包含一种对象的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;
        }

    }

城市课程:

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

    }

州级:

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?



    }

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以查看Comparable界面。 Here是文档。