A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 騛鹏 中级黑马   /  2013-4-6 19:18  /  1752 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 騛鹏 于 2013-4-6 21:11 编辑
  1. import java.util.*;
  2. import java.util.TreeSet;

  3. class Student implements Comparable<Student>
  4. {
  5.         private String name;
  6.         private int age;

  7.         Student(String name, int age)
  8.         {
  9.                 this.name = name;
  10.                 this.age = age;
  11.         }

  12.         public int compareTo(Student s)
  13.         {
  14.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  15.                 if(num==0)
  16.                         return this.name.compareTo(s.name);
  17.                 return num;
  18.         }
  19.         public int hashCode()
  20.         {
  21.                 return name.hashCode()+age*34;
  22.         }
  23.         
  24.         public boolean equals(Object obj)
  25.         {
  26.                 if(!(obj instanceof Student))
  27.                         throw new ClassCastException("leixing bu pipei");
  28.                 Student s = (Student)obj;
  29.                 return this.name.equals(s.name)&&this.age==s.age;
  30.         }
  31.         public String getName()
  32.         {
  33.                 return name;
  34.         }
  35.         public int getAge()
  36.         {
  37.                 return age;
  38.         }
  39.         public String toString()
  40.         {
  41.                 return name+":"+age;
  42.         }
  43. }
  44. class  MapTest
  45. {
  46.         public static void main(String[] args)
  47.         {
  48.                 HashMap<Student,String> hm = new HashMap<Student,String>();

  49.                 hm.put(new Student("list1",21),"beijing");
  50.                 hm.put(new Student("list1",21),"tianjin");
  51.                 hm.put(new Student("list2",22),"shanghai");
  52.                 hm.put(new Student("list3",23),"nanjing");
  53.                 hm.put(new Student("list4",24),"wuhan");
  54. /*                //第一种取出方式 :keySet
  55.                 //先获取map集合的所有键的Set集合,keySet();
  56.                 Set<Student> keySet = hm.keySet();

  57.                 //有了Set集合。就可以获取其迭代器。
  58.                 Iterator<Student> it = keySet.iterator();
  59.                         
  60.                 while (it.hasNext())
  61.                 {
  62.                         Student stu = it.next();
  63.                         //有了键可通过map集合get方法获取对应的值。
  64.                         String addr = hm.get(stu);        
  65.                         System.out.println(stu+".."+addr);
  66.                 }
  67. */
  68.                 //第二种方式: EntrySet
  69.                 //将Map集合中的映射关系取出。存入到Set集合中
  70.                 Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
  71.                         
  72.                 //TreeSet<Map.Entry<Student,String>> treeSet = new TreeSet<Map.Entry<Student,String>>(hm);
  73.                
  74.                 Iterator<Map.Entry<Student,String>> it = entrySet.iterator();

  75.                 while (it.hasNext())
  76.                 {
  77.                         Map.Entry<Student,String> me = it.next();

  78.                         Student stu = me.getKey();
  79.                         String addr = me.getValue();
  80.                         System.out.println(stu+"::::::::"+addr);
  81.                 }
  82.         }
  83.         
  84. }
复制代码
本例中如何使用TreeSet()排序?改动可以,关键是 以实现 compareble的方式来排序,而非定义比较器。

//TreeSet<Map.Entry<Student,String>> treeSet = new TreeSet<Map.Entry<Student,String>>(hm);
这句是错误的。

TreeSet(Collection<? extends E> c)
构造一个包含指定 collection 元素的新 TreeSet,它按照其元素的自然顺序进行排序。

为什么不可以?   

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

7 个回复

倒序浏览
TreeSet接收的集合必须具备比较性,而Map.Entry不具备比较性,所以不能拿TreeSet接收。
要实现排序就把HashMap改为TreeMap,就能利用实现了Comparable接口的Student自身来排序了。因为hashmap是无序的。

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
第一个问题:如何使用TreeSet()排序?你比较写的也正确,只是在后边遍历时整错,还有就是比较器代码觉得可改进,可参考:
我写的比较方法如下:
  1.         public int compareTo(Student s) {
  2.                 // int num = this.name.compareTo(s.name);
  3.                 // return num == 0 ? this.age - s.age : num;
  4.                 int num = this.age - s.age;
  5.                 return num == 0 ? this.name.compareTo(s.name) : num;
  6.                 // return this.age - s.age;
  7.         }
复制代码
遍历代码:
  1. public class TreeSetDemo2 {
  2.         public static void main(String[] args) {
  3.                 TreeSet<Student> ts = new TreeSet<Student>();

  4.                 Student s1 = new Student("李白", 28);
  5.                 Student s2 = new Student("杜甫", 25);
  6.                 Student s3 = new Student("李白", 28);
  7.                 Student s4 = new Student("白居易", 23);
  8.                 Student s5 = new Student("杜甫", 25);
  9.                 Student s6 = new Student("李清照", 25);
  10.                 Student s7 = new Student("李照", 35);
  11.                 Student s8 = new Student("李", 27);

  12.                 ts.add(s1);
  13.                 ts.add(s2);
  14.                 ts.add(s3);
  15.                 ts.add(s4);
  16.                 ts.add(s5);
  17.                 ts.add(s6);
  18.                 ts.add(s7);
  19.                 ts.add(s8);

  20.                 Iterator<Student> it = ts.iterator();
  21.                 while (it.hasNext()) {
  22.                         Student s = it.next();
  23.                         System.out.println(s.getName() + "***" + s.getAge());
  24.                 }
  25.         }
  26. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
存放具有比较性的元素你用HashSet那不是没事找事么,要不人家定义一个TreeSet干嘛呢 这两的用处区别就在这里 如果存的元素需要有比较性 就用TreeSet。不需要元素具有比较性时首选HashSet  这个效率会相对高一些

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
HM朱俊 发表于 2013-4-6 20:29
TreeSet接收的集合必须具备比较性,而Map.Entry不具备比较性,所以不能拿TreeSet接收。
要实现排序就把Hash ...

谢谢   改为TreeMap可以的  
且无需复写 equals()  hashCode()

能否简要说下 TreeMap如何实现排序的?
回复 使用道具 举报
由对象实现Comparable接口或集合实现Comparator接口的CompareTo()方法,从而实现排序
如下例:实现了Comparable接口
  1. public class TreeMapDemo {
  2.         public static void main(String[] args) {
  3.                 TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>() {
  4.                         @Override
  5.                         public int compare(Student s1, Student s2) {
  6.                                 int num = s1.getAge() - s2.getAge();
  7.                                 return num == 0 ? s1.getName().compareTo(s2.getName()) : num;
  8.                         }
  9.                 });

  10.                 Student s1 = new Student("上官金红", 40);
  11.                 Student s2 = new Student("李寻欢", 28);
  12.                 Student s3 = new Student("天机老人", 36);
  13.                 Student s4 = new Student("阿飞", 32);

  14.                 tm.put(s1, "北京");
  15.                 tm.put(s2, "上海");
  16.                 tm.put(s3, "钓鱼岛");
  17.                 tm.put(s4, "香港");

  18.                 Set<Student> set = tm.keySet();
  19.                 Iterator<Student> it = set.iterator();
  20.                 while (it.hasNext()) {
  21.                         Student key = it.next();
  22.                         String value = tm.get(key);
  23.                         System.out.println(key.getName() + "***" + key.getAge() + "***" + value);
  24.                 }
  25.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
騛鹏 发表于 2013-4-6 20:54
谢谢   改为TreeMap可以的  
且无需复写 equals()  hashCode()

TreeMap是根据该映射关系中键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。跟TreeSet原理一样的。如果键不具备比较性就要让元素实现Comparable接口覆盖compareTo方法,或者定义一个比较器是覆盖Comparetor中的compare方法
回复 使用道具 举报
HM朱俊 发表于 2013-4-6 21:16
TreeMap是根据该映射关系中键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体 ...

谢谢  同样谢谢楼上的回复者
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马