集合框架-Map集合-TreeMap存储自定义对象
1 package cn.itcast.p8.treemap.demo; 2 3 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.TreeMap; 7 8 import cn.itcast.p2.bean.Student; 9 import cn.itcast.p3.comparator.ComparatorByName; 10 11 public class TreeMapDemo { 12 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName()); 16 tm.put(new Student("lisi",38), "北京"); 17 tm.put(new Student("zhaoliu",24), "上海"); 18 tm.put(new Student("xiaoqiang",31), "沈阳"); 19 tm.put(new Student("wangcai",28), "大连"); 20 tm.put(new Student("zhaoliu",24), "铁岭"); 21 22 Iterator<Map.Entry<Student, String>> it = tm.entrySet().iterator(); 23 24 25 while (it.hasNext()) { 26 Map.Entry<Student, String> me = it.next(); 27 Student key = me.getKey(); 28 String value = me.getValue(); 29 System.out.println(key.getName()+":"+key.getAge()+"---"+value); 30 } 31 } 32 33 }
1 package cn.itcast.p2.bean; 2 3 public class Person implements Comparable<Person>{ 4 private String name; 5 private int age; 6 7 8 public Person() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 13 public Person(String name, int age) { 14 super(); 15 this.name = name; 16 this.age = age; 17 } 18 public int compareTo(Person p) {//指定比较类型,传入比较类型 19 // Person p= (Person)obj; 20 int temp = this.age-p.age; 21 return temp==0?this.name.compareTo(p.name):temp; 22 } 23 24 25 @Override 26 public int hashCode() { 27 final int prime = 31; 28 int result = 1; 29 result = prime * result + age; 30 result = prime * result + ((name == null) ? 0 : name.hashCode()); 31 return result; 32 } 33 34 @Override 35 public boolean equals(Object obj) { 36 if (this == obj) 37 return true; 38 if (obj == null) 39 return false; 40 if (getClass() != obj.getClass()) 41 return false; 42 Person other = (Person) obj; 43 if (age != other.age) 44 return false; 45 if (name == null) { 46 if (other.name != null) 47 return false; 48 } else if (!name.equals(other.name)) 49 return false; 50 return true; 51 } 52 53 // @Override 54 // public boolean equals(Object obj) {//不能把Object改为Person,equals方法来自Object,Object没有定义泛型 55 // // TODO Auto-generated method stub 56 // if (this == obj) { 57 // return true; 58 // } 59 // if (!(obj instanceof Person)) { 60 // throw new RuntimeException() 61 // } 62 // Person p = (Person)obj; 63 // return super.equals(obj); 64 // } 65 /* 66 * @Override public int hashCode() { final int prime = 31; int result = 1; 67 * result = prime * result + age; result = prime * result + ((name == null) ? 0 68 * : name.hashCode()); return result; } 69 * 70 * @Override public boolean equals(Object obj) { if (this == obj) return true; 71 * if (obj == null) return false; if (getClass() != obj.getClass()) return 72 * false; Person other = (Person) obj; if (age != other.age) return false; if 73 * (name == null) { if (other.name != null) return false; } else if 74 * (!name.equals(other.name)) return false; return true; }//alt+shift+s 75 * hashCode和equals 76 */ 77 public String getName() { 78 return name; 79 } 80 public void setName(String name) { 81 this.name = name; 82 } 83 public int getAge() { 84 return age; 85 } 86 public void setAge(int age) { 87 this.age = age; 88 } 89 90 @Override 91 public String toString() { 92 return "Person:"+getName()+":"+getAge(); 93 } 94 95 96 }
1 package cn.itcast.p2.bean; 2 3 public class Student extends Person { 4 5 public Student() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public Student(String name, int age) { 11 super(name, age); 12 // TODO Auto-generated constructor stub 13 } 14 15 @Override 16 public String toString() { 17 // TODO Auto-generated method stub 18 return "Student:"+getName()+":"+getAge(); 19 } 20 21 }
1 package cn.itcast.p3.comparator; 2 3 import java.util.Comparator; 4 5 import cn.itcast.p2.bean.Person; 6 7 public class ComparatorByName implements Comparator<Person> {//ctrl+1快速实现方法 8 9 @Override 10 public int compare(Person o1, Person o2) { 11 // TODO Auto-generated method stub 12 int temp = o1.getName().compareTo(o2.getName()); 13 return temp==0?o1.getAge()-o2.getAge():temp; 14 } 15 16 }