本帖最后由 刘 佳 于 2012-10-30 15:46 编辑
李连闯 发表于 2012-10-30 15:24
代码贴出来看下,浪费你时间了抱歉,
- <P>import java.util.*;
- class GenericDemo7
- {
- public static void main(String[] args)
- {
-
- TreeSet<Student> ts = new TreeSet<Student>(new Comp());</P>
- <P> ts.add(new Student("abc03"));
- ts.add(new Student("abc02"));
- ts.add(new Student("abc06"));
- ts.add(new Student("abc01"));
-
- Iterator<Student> it = ts.iterator();</P>
- <P> while(it.hasNext())
- {
- System.out.println(it.next().getName());
- }
- </P>
- <P> </P>
- <P> TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());</P>
- <P> ts1.add(new Worker("wabc--03"));
- ts1.add(new Worker("wabc--02"));
- ts1.add(new Worker("wabc--06"));
- ts1.add(new Worker("wabc--01"));</P>
- <P>
- Iterator<Worker> it1 = ts1.iterator();</P>
- <P> while(it1.hasNext())
- {
- System.out.println(it1.next().getName());
- }
- }
- }</P>
- <P>class Comp implements Comparator<Person>
- {
- public int compare(Person p1,Person p2)
- {
- return p2.getName().compareTo(p1.getName());
- }
- }</P>
- <P>
- class Person
- {
- private String name;
- Person(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- public String toString()
- {
- return "person :"+name;
- }
- }</P>
- <P>class Student extends Person
- {
- Student(String name)
- {
- super(name);
- }</P>
- <P>}</P>
- <P>class Worker extends Person
- {
- Worker(String name)
- {
- super(name);
- }
- }
- </P>
复制代码 |