- package 集合框架1;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.List;
- import java.util.TreeMap;
- class Student implements Comparable<Student>
- {
- private String name;
- private int age;
-
- Student(String name, int age)
- {
- this.setName(name);
- this.setAge(age);
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int compareTo(Student stu) {
- if(stu.age>this.age)
- {
- return 1;
- }
-
- if(stu.age<this.age)
- {
- return -1;
- }
- return stu.name.compareTo(this.name);
-
-
-
- }
-
-
-
-
- }
- public class MapDemo2 {
- public static void main(String[] args) {
- TreeMap<String , List> jiangzhi = new TreeMap<String , List>();
-
- ArrayList<Student> yidong = new ArrayList<Student>();
- ArrayList<Student> diansheng = new ArrayList<Student>();
-
- yidong.add(new Student("张翔",45));
- yidong.add(new Student("犬夜叉",20));
- yidong.add(new Student("张翔",45));
-
- diansheng.add(new Student("刘翔",40));
- diansheng.add(new Student("子健",20));
- diansheng.add(new Student("刘翔",40));
-
- jiangzhi.put("yidong", yidong);
- jiangzhi.put("diansheng", diansheng);
-
- jiangzhiIterator(jiangzhi);
-
-
- }
- private static void jiangzhiIterator(TreeMap<String, List> jiangzhi) {
- // TODO Auto-generated method stub
- Iterator<String> it = jiangzhi.keySet().iterator();
-
- while(it.hasNext()){
- String key = it.next();
- List<Student> room = jiangzhi.get(key);
- System.out.println("班级:"+key);
- roomIterator(room);
- }
-
-
- }
- private static void roomIterator(List room) {
- // TODO Auto-generated method stub
- Iterator it = room.iterator();
-
- while(it.hasNext())
- {
- Student stu = (Student) it.next();
- System.out.println(stu.getName()+"..."+stu.getAge());
- }
- }
- }
复制代码
为什么不能去重????? |
|