黑马程序员技术交流社区

标题: TreeMap集合存储自定义对象演示 [打印本页]

作者: 小穿钉    时间: 2015-11-20 20:10
标题: TreeMap集合存储自定义对象演示
package cn.map;


import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;


/**
* TreeMap集合存储自定义对象演示
*
* @author Administrator
*
*/


public class TreeMapDemo {


        public static void main(String[] args) {
                // 创建TreeMap集合对象,并明确泛型类型,传入按照年龄排序的新的比较器
                TreeMap<Seaman, Address> treeMap = new TreeMap<Seaman, Address>(new NewComparatorByAge());
                // 调用方法,获取按照年龄排序的船员信息(姓名年龄地址)
                getSeamanInformation(treeMap);


        }


        private static void getSeamanInformation(TreeMap<Seaman, Address> treeMap) {
                // 向TreeMap集合中添加键值对
                treeMap.put((new Seaman("黎明", 23)), (new Address("山东", "济南", "历下区")));
                treeMap.put((new Seaman("李钢", 29)), (new Address("山东", "淄博", "沂源")));
                treeMap.put((new Seaman("王磊", 26)), (new Address("山东", "聊城", "茌平")));
                treeMap.put((new Seaman("黎明", 21)), (new Address("山东", "济南", "历下区")));


                // 获取键值对映射关系的对象的Set集合,并对其进行迭代
                Iterator<Map.Entry<Seaman, Address>> it = treeMap.entrySet().iterator();
                while (it.hasNext()) {
                        Map.Entry<Seaman, Address> mapEntry = it.next();
                        // 获取键
                        Seaman key = mapEntry.getKey();
                        // 获取值
                        Address value = mapEntry.getValue();
                        System.out.println(key + "" + value);
                }


        }


}


/*
* 创建比较器,按照年龄进行排序
*/
class NewComparatorByAge implements Comparator<Seaman> {
        // 重写compare方法
        @Override
        public int compare(Seaman o1, Seaman o2) {
                int temp = o1.getAge() - o2.getAge();
                return temp == 0 ? o1.getName().compareTo(o2.getName()) : temp;
        }


}


/*
* 船员类,属性有姓名和年龄
*/
class Seaman {
        // 私有化成员变量
        private String name;
        private int age;


        // 构造方法初始化对象
        public Seaman(String name, int age) {
                super();
                this.name = name;
                this.age = age;
        }


        // 重写hashCode方法,为了保证唯一性,如果相同就再重写equals方法
        @Override
        public int hashCode() {
                return name.hashCode() + age * 13;
        }
        // set get 方法


        public String getName() {
                return name;
        }


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


        public int getAge() {
                return age;
        }


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


        // 重写equals方法
        @Override
        public boolean equals(Object obj) {
                // 判断是不是同一个对象
                if (this == obj)
                        return true;
                Seaman stu = (Seaman) obj;
                return this.name.equals(stu.name) && this.age == stu.age;
        }


        // 重写toString方法
        @Override
        public String toString() {
                return "姓名:" + name + "  年龄:" + age;
        }
}


/*
* 籍贯类,属性有省份、城市、乡镇
*/
class Address {
        // 私有化成员变量:省份、城市、乡镇
        private String province;
        private String city;
        private String village;


        // 构造方法初始化对象
        public Address(String province, String city, String village) {
                super();
                this.province = province;
                this.city = city;
                this.village = village;
        }


        // 重写toString方法
        @Override
        public String toString() {
                return "  籍贯: " + province + "省" + city + "市" + village + "县";
        }


}


求花花


作者: 陈先森    时间: 2015-11-20 21:30

作者: yaoyeqing    时间: 2015-11-20 21:56

作者: 小穿钉    时间: 2015-11-20 22:34
陈先森 发表于 2015-11-20 21:30

Thank  you
作者: 小穿钉    时间: 2015-11-20 22:36
yaoyeqing 发表于 2015-11-20 21:56

Thank  you




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2