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

© noiary 高级黑马   /  2014-9-23 23:38  /  1334 人查看  /  5 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;

  2. /* Set 无序,不可以重复元素.
  3.         |--HashSet
  4.                 数据结构式哈希表.线程时非同步的.
  5.                 保证元素唯一性的原理:判断元素的hashCode值是否相同.
  6.                 如果相同,还会继续判断元素的equals方法,是否为true.
  7.         |--TreeSet
  8.                 可以对Set集合中的元素进行排序.
  9.                 底层数据结构是二叉树.
  10.                 保证元素唯一性的依据:
  11.                         compareTo方法 return 0.
  12.                 TreeSet排序的第一种方式:让元素自身具备比较性.
  13.                 元素需要实现comparable接口,覆盖compareTo方法.
  14.                 这种方式也成为自然顺序,或者叫默认顺序.
  15. */

  16. class TreeSetDemo {

  17.         public static void main(String[] args) {
  18.                 TreeSet ts = new TreeSet();
  19.                
  20.                 ts.add(new Student("lisi02",22));
  21.                 ts.add(new Student("lisi002",20));
  22.                 ts.add(new Student("lisi09",19));
  23.                 ts.add(new Student("lisi08",19));
  24.                
  25.                 for(Iterator it = ts.iterator(); it.hasNext(); ) {
  26.                
  27.                         Student s = (Student)it.next();
  28.                         sop(s.getName() + "===" + s.getAge());
  29.                 }
  30.                
  31.         }
  32.        
  33.         public static void sop(Object obj) {
  34.                 System.out.println(obj);
  35.         }
  36. }

  37. class Student implements Comparable {

  38.         private String name;
  39.         private int age;
  40.        
  41.         public Student(String name, int age) {
  42.                 this.name = name;
  43.                 this.age = age;
  44.         }
  45.        
  46.         public String getName() {
  47.                 return name;
  48.         }
  49.        
  50.         public int getAge() {
  51.                 return age;
  52.         }
  53.        
  54.         public int compareTo(Object obj) {
  55.                 if(!(obj instanceof Student))
  56.                         throw new RuntimeException("不是学生对象!");
  57.                        
  58.                 Student s = (Student)obj;
  59.                
  60.                 if(this.age > s.age)
  61.                         return 1;
  62.                        
  63.                 if(this.age == s.age)
  64.                         return this.name.compareTo(s.name);
  65.                        
  66.                 return -1;
  67.         }
  68. }
复制代码
  1. import java.util.*;
  2. /*
  3. 当元素自身不具备比较性,或者具备的比较性不是所需要的。
  4. 这时需要让容器自身具备比较性。
  5. 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。

  6. 当两种排序都存在时,以比较器为主。

  7. 定义一个类,实现Comparator接口,覆盖compare方法。


  8. */

  9. public class TreeSetDemo2 {

  10.         public static void main(String[] args) {
  11.                 TreeSet ts = new TreeSet(new MyCompare());
  12.                
  13.                 ts.add(new Student("Hu",20));
  14.                 ts.add(new Student("Li",25));
  15.                 ts.add(new Student("Zheng",28));
  16.                 ts.add(new Student("Wang",29));
  17.                 ts.add(new Student("Song",21));
  18.                 ts.add(new Student("Song",21));
  19.                
  20.                 for(Iterator it = ts.iterator(); it.hasNext(); ) {
  21.                         Student s = (Student)it.next();
  22.                         TreeSetDemo.sop(s.getName()+"==="+s.getAge());
  23.                 }
  24.         }
  25. }

  26. class MyCompare implements Comparator {

  27.         public int compare(Object o1, Object o2) {
  28.        
  29.                 Student s1 = (Student)o1;
  30.                 Student s2 = (Student)o2;
  31.                
  32.                 Integer num = s1.getName().compareTo(s2.getName());
  33.                
  34.                 if(num == 0)
  35.                         return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  36.                
  37.                 return num;
  38.         }
  39. }
复制代码
  1. import java.util.*;
  2. /*
  3. 练习:按照字符串长度排序。

  4. 字符串本身具备比较性。但是它的比较方式不是所需要的。

  5. 这时就只能使用比较器。



  6. */
  7. public class TreeSetTest {

  8.         public static void main(String[] args) {
  9.        
  10.                 TreeSet ts = new TreeSet(new StrLenComparator());
  11.                
  12.                 ts.add("abcd");
  13.                 ts.add("cc");
  14.                 ts.add("cba");
  15.                 ts.add("aaa");
  16.                 ts.add("z");
  17.                 ts.add("hahaha");
  18.                
  19.                 for(Iterator it = ts.iterator(); it.hasNext(); ) {
  20.                         TreeSetDemo.sop(it.next());
  21.                 }
  22.         }
  23. }

  24. class StrLenComparator implements Comparator {

  25.         public int compare(Object o1, Object o2) {
  26.                 String s1 = (String)o1;
  27.                 String s2 = (String)o2;
  28.                
  29.                 int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
  30.                 if(num == 0)
  31.                         return s1.compareTo(s2);
  32.                        
  33.                 return num;
  34.         }
  35. }
复制代码

5 个回复

倒序浏览
强悍啊,总结的这么完全??
回复 使用道具 举报
苏乞儿 发表于 2014-9-23 23:54
强悍啊,总结的这么完全??

{:3_64:} 跟着视频做的
回复 使用道具 举报
很用心啊~~~加油
回复 使用道具 举报
很好的总结,一起努力,加油吧!!
回复 使用道具 举报
自己写一遍印象更深刻
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马