Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258242
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2014-05-15 21:50:41



点击(此处)折叠或打开

  1. import java.util.*;
  2. class GenericDemo7
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         
  7.         TreeSet<Student> ts = new TreeSet<Student>(new Comp());

  8.         ts.add(new Student("abc03"));
  9.         ts.add(new Student("abc02"));
  10.         ts.add(new Student("abc06"));
  11.         ts.add(new Student("abc01"));
  12.         
  13.         Iterator<Student> it = ts.iterator();

  14.         while(it.hasNext())
  15.         {
  16.             System.out.println(it.next().getName());
  17.         }
  18.         /**/



  19.         TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());

  20.         ts1.add(new Worker("wabc--03"));
  21.         ts1.add(new Worker("wabc--02"));
  22.         ts1.add(new Worker("wabc--06"));
  23.         ts1.add(new Worker("wabc--01"));


  24.         Iterator<Worker> it1 = ts1.iterator();

  25.         while(it1.hasNext())
  26.         {
  27.             System.out.println(it1.next().getName());
  28.         }
  29.     }
  30. }

  31. /*
  32. class StuComp implements Comparator
  33. {
  34.     public int compare(Student s1,Student s2)
  35.     {
  36.         return s1.getName().compareTo(s2.getName());
  37.     }
  38. }

  39. class WorkerComp implements Comparator
  40. {
  41.     public int compare(Worker s1,Worker s2)
  42.     {
  43.         return s1.getName().compareTo(s2.getName());
  44.     }
  45. }
  46. */

  47. class Comp implements Comparator<Person>
  48. {
  49.     public int compare(Person p1,Person p2)
  50.     {
  51.         return p2.getName().compareTo(p1.getName());
  52.     }
  53. }


  54. class Person
  55. {
  56.     private String name;
  57.     Person(String name)
  58.     {
  59.         this.name = name;
  60.     }
  61.     public String getName()
  62.     {
  63.         return name;
  64.     }
  65.     public String toString()
  66.     {
  67.         return "person :"+name;
  68.     }
  69. }

  70. class Student extends Person
  71. {
  72.     Student(String name)
  73.     {
  74.         super(name);
  75.     }

  76. }

  77. class Worker extends Person
  78. {
  79.     Worker(String name)
  80.     {
  81.         super(name);
  82.     }
  83. }

阅读(384) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~