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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 路文龙 中级黑马   /  2015-3-9 11:29  /  739 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

、首先明确TreeSet与HashSet的区别:
1、底层实现不同:TreeSet通过TreeMap实现,数据结构为二叉树,HashSet通过HashMap实现,只通过key值来实现,数据结构为哈希表。
2、共同点是集合的唯一性。
3、不同点事TreeSet是有序的,HashSet是无序的关于HashSet的唯一性实现详见[url=学习分享 hashcode()与equals() 基于哈希表的集合HashSet http://bbs.itheima.com/thread-174387-1-1.html (出处: 黑马程序员IT技术论坛)]hashcode()与equals() 学习[/url]。
二、TreeSet有序性的实现方法
1、通过在定义类的时候实现Comparable接口来实现:
comparable实现类的子

示例代码:
  1. import java.util.*;

  2. class TreeSetDemo2  
  3. {
  4.     public static void main(String[] args)  
  5.     {
  6.         TreeSet ts = new TreeSet();

  7.         ts.add(new Student("lisi0",30));
  8.         ts.add(new Student("lisixx",29));
  9.         ts.add(new Student("lisi9",29));
  10.         ts.add(new Student("lisi8",38));
  11.         ts.add(new Student("lisixx",29));
  12.         ts.add(new Student("lisi4",14));
  13.         //ts.add(new Student(39));
  14.         ts.add(new Student("lisi7",27));



  15.         System.out.println(ts);
  16.     }
  17. }

  18. //同姓名同年龄的学生视为同一个学生。按照学生的年龄排序。
  19. class Student implements Comparable
  20. {
  21.     private int age;
  22.     private String name;
  23.     Student(String name,int age)
  24.     {
  25.         this.age = age;
  26.         this.name = name;
  27.     }

  28.     public int compareTo(Object obj)
  29.     {
  30.         Student stu = (Student)obj;
  31.         int num = new Integer(this.age).compareTo(new Integer(stu.age));
  32.         return num==0?this.name.compareTo(stu.name):num;
  33.         /*
  34.         if(this.age>stu.age)
  35.             return 1;
  36.         if(this.age==stu.age)
  37.             return this.name.compareTo(stu.name);
  38.         return -1;
  39.         */
  40.         /**/
  41.     }
  42.     public int getAge()
  43.     {
  44.         return age;
  45.     }
  46.     public String toString()
  47.     {
  48.         return name+"::"+age;
  49.     }
  50. }
复制代码
但是,如果元素自身不具备比较性,或者元素自身具备的比较性,不是所需要的。
比如,学生的自然排序是按年龄排序,现在想要按照学生姓名排序。还可以不改动原有代码。
这时怎么办呢?
排序的第二种方式:自定比较器的方式。
这时可以让集合自身具备比较性。
可以定义一个类实现Comparator接口,覆盖compare方法。将该Comparator接口子类对象作为实际参数
传递给TreeSet集合构造函数。该对象就是比较器。
  1. import java.util.*;
  2. class TreeSetDemo3  
  3. {
  4.     public static void main(String[] args)  
  5.     {
  6.         TreeSet ts = new TreeSet(new StudentComparatorByName());
  7.         ts.add(new Student("lisi0",30));
  8.         ts.add(new Student("lisixx",29));
  9.         ts.add(new Student("lisi9",29));
  10.         ts.add(new Student("lisi8",38));
  11.         ts.add(new Student("lisixx",29));
  12.         ts.add(new Student("lisi4",14));
  13.         //ts.add(new Student(39));
  14.         ts.add(new Student("lisi7",27));
  15.         System.out.println(ts);
  16.     }
  17. }
  18. class StudentComparatorByName implements Comparator
  19. {
  20.     public int compare(Object o1,Object o2)
  21.     {
  22.         Student s1 = (Student)o1;
  23.         Student s2 = (Student)o2;
  24.         int num = s1.getName().compareTo(s2.getName());
  25.         return num==0?new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())):num;
  26.     }
  27. }
  28. //同姓名同年龄的学生视为同一个学生。按照学生的年龄排序。
  29. class Student implements Comparable
  30. {
  31.     private int age;
  32.     private String name;
  33.     Student(String name,int age)
  34.     {
  35.         this.age = age;
  36.         this.name = name;
  37.     }
  38.     public int compareTo(Object obj)
  39.     {
  40.         Student stu = (Student)obj;
  41.         int num = new Integer(this.age).compareTo(new Integer(stu.age));
  42.         return num==0?this.name.compareTo(stu.name):num;
  43.         /*
  44.         if(this.age>stu.age)
  45.             return 1;
  46.         if(this.age==stu.age)
  47.             return this.name.compareTo(stu.name);
  48.         return -1;
  49.         */
  50.         /**/
  51.     }
  52.     public int getAge()
  53.     {
  54.         return age;
  55.     }
  56.     public String getName()
  57.     {
  58.         return name;
  59.     }
  60.     public String toString()
  61.     {
  62.         return name+"::"+age;
  63.     }
  64. }

复制代码



评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1 好好学习!!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马