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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王自强 中级黑马   /  2012-8-30 21:43  /  1744 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王自强 于 2012-8-31 15:19 编辑

  1. import java.util.*;
  2. class Person implements Comparable
  3. {
  4.         private String name;
  5.         private int age;
  6.         Person(String name,int age)
  7.         {
  8.                 this.name = name;
  9.                 this.age = age;
  10.         }
  11.         public boolean equals(Object obj)
  12.         {
  13.                 if(!(obj instanceof Person))
  14.                         return false;
  15.                 Person p = (Person)obj;
  16.                 return this.name==p.name && this.age==p.age;
  17.         }
  18.         public int hashCode()
  19.         {
  20.                 return this.name.hashCode()+this.age*33;
  21.         }
  22.         
  23.         public int compareTo(Object obj)
  24.         {
  25.                 if(!(obj instanceof Person))
  26.                         throw new RuntimeException();
  27.                 Person p = (Person)obj;
  28.                 int num = this.name.compareTo(p.name);
  29.                 if(num == 0)
  30.                         return (this.age-p.age);
  31.                         //return  new Integer(this.age).compareTo(new Integer(p.Age));
  32.                         //这两句有什么区别吗?
  33.                 return num;
  34.         }

  35.         public void setName(String name)
  36.         {
  37.                 this.name = name;
  38.         }
  39.         public String getName()
  40.         {
  41.                 return this.name;
  42.         }
  43.         public void setAge(int age)
  44.         {
  45.                 this.age = age;
  46.         }
  47.         public int getAge()
  48.         {
  49.                 return this.age;
  50.         }
  51. }
  52. class demo
  53. {
  54.         public static void sop(Object obj)
  55.         {
  56.                 System.out.println(obj);
  57.         }
  58.         public static void main(String[] args)
  59.         {
  60.                 TreeSet ts = new TreeSet(new myCompare());

  61.                 ts.add(new Person("abc",34));
  62.                 ts.add(new Person("zzt",44));
  63.                 ts.add(new Person("ace",44));
  64.                 ts.add(new Person("hhh",54));

  65.                 ts.add(new Person("ace",16));
  66.                 ts.add(new Person("zrt",24));

  67.                 for(Iterator it = ts.iterator();it.hasNext();)
  68.                 {
  69.                         Person p = (Person)it.next();
  70.                         sop(p.getName()+"::"+p.getAge());
  71.                 }
  72.         }        
  73. }
  74. class myCompare implements Comparator
  75. {
  76.         public int compare(Object obj1,Object obj2)
  77.         {
  78.                 Person p1 = (Person)obj1;
  79.                 Person p2 = (Person)obj2;

  80.                 int num = p1.getAge()-p2.getAge();
  81.                 //int num = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
  82.                 //这两句有什么区别吗?

  83.                 if(num == 0)
  84.                         return p1.getName().compareTo(p2.getName());
  85.                 return num;
  86.         }
  87. }
复制代码

2 个回复

倒序浏览
本帖最后由 周兴华 于 2012-8-30 22:04 编辑

return (this.age-p.age);
//return new Integer(this.age).compareTo(new Integer(p.Age));
//这两句有什么区别吗?


int是基本数据类型,Integer是int的包装类,Integer类实现了Comparable接口,因此Integer可调用compareTo方法,下面的代码也是同理。

int num = p1.getAge()-p2.getAge();

//int num = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));

//这两句有什么区别吗?
回复 使用道具 举报
int num = p1.getAge()-p2.getAge();
int num = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));

这两句都表示两个对象年龄值的大小.第一句采用了数学减法的形式进行了比较,值有三种情况,等于0,小于0大于0

第二句采用的是compareTo方法进行比较,因为Integer具有该方法,这里把int型数据装箱成了Integer类型

这两者的效果一样
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马