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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 可见 中级黑马   /  2012-3-26 19:58  /  3944 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. public class StudentDemo implements Comparable<StudentDemo>
  2. {
  3.         private String name;
  4.         private int age;
  5.         private int score;
  6.         StudentDemo(String name,int age,int score)
  7.         {
  8.                 this.name = name;
  9.                 this.age = age;
  10.                 this.score = score;
  11.         }
  12.         public void setName(String name)
  13.         {
  14.                 this.name = name;
  15.         }
  16.         public void setAge(int age)
  17.         {
  18.                 this.age = age;
  19.         }
  20.         public void setScore()
  21.         {
  22.                 this.score = score;
  23.         }
  24.         public String getName()
  25.         {
  26.                 return name;
  27.         }
  28.         public int getAge()
  29.         {
  30.                 return age;
  31.         }
  32.         public int getScore()
  33.         {
  34.                 return score;
  35.         }
  36.                 public int compareTo(StudentDemo s)
  37.         {
  38.                 int num = new Integer(this.score).compareTo(new Integer(s.score));
  39.                
  40.                 if(num == 0)
  41.                 {
  42.                         return this.name.compareTo(s.name);
  43.                 }
  44.                 return num;
  45.         }
  46.        
  47.        
  48.        
  49.        
  50. }


  51. import java.util.*;

  52. public class TreeSetSort
  53. {

  54.        
  55.         public static void main(String[] args)
  56.         {
  57.                 TreeSet<StudentDemo> ts = new TreeSet<StudentDemo>();
  58.                 ts.add(new StudentDemo("zhangsan2",22,80));
  59.                 ts.add(new StudentDemo("zhangsan2",22,80));
  60.                 //ts.add(new StudentDemo("zhangsan1",20,90));
  61.                 //ts.add(new StudentDemo("zhangsan3",24,70));
  62.                 //ts.add(new StudentDemo("zhangsan4",19,95));
  63.                 //ts.add(new StudentDemo("zhangsan5",28,63));

  64.                
  65.                 Iterator it = ts.iterator();
  66.                 while(it.hasNext())
  67.                 {
  68.                         StudentDemo stu = (StudentDemo)it.next();
  69.                         System.out.println(stu.getName()+":"+stu.getAge()+"..."+stu.getScore());
  70.                 }
  71.         }
  72.        

  73. }





复制代码
我添加了2个元素,都是一样的,打印只出现一个。是哪一部分代码的功能是屏蔽相同的?

9 个回复

倒序浏览

  1. public class StudentDemo implements Comparable<StudentDemo>
  2. {
  3.         private String name;
  4.         private int age;
  5.         private int score;
  6.         StudentDemo(String name,int age,int score)
  7.         {
  8.                 this.name = name;
  9.                 this.age = age;
  10.                 this.score = score;
  11.         }
  12.         public void setName(String name)
  13.         {
  14.                 this.name = name;
  15.         }
  16.         public void setAge(int age)
  17.         {
  18.                 this.age = age;
  19.         }
  20.         public void setScore()
  21.         {
  22.                 this.score = score;
  23.         }
  24.         public String getName()
  25.         {
  26.                 return name;
  27.         }
  28.         public int getAge()
  29.         {
  30.                 return age;
  31.         }
  32.         public int getScore()
  33.         {
  34.                 return score;
  35.         }
  36.         /*public int hashCode()
  37.         {
  38.                 return name.hashCode()+age+score;
  39.         }
  40.         public boolean equals(Object obj)
  41.         {
  42.                 if(!(obj instanceof StudentDemo))
  43.                 {
  44.                         throw new ClassCastException("类型不匹配");
  45.                 }
  46.                 StudentDemo s = (StudentDemo)obj;
  47.                
  48.                 return this.name.equals(s.name) && this.age == s.age && this.score == s.score;
  49.         }
  50.         */
  51.        
  52.         public int compareTo(StudentDemo s)
  53.         {
  54.                 int num = new Integer(this.score).compareTo(new Integer(s.score));
  55.                
  56.                 if(num == 0)
  57.                 {
  58.                         return this.name.compareTo(s.name);
  59.                 }
  60.                 return num;
  61.         }
  62.        
  63.        
  64.        
  65.        
  66. }

  67. import java.util.*;

  68. public class TreeSetSort
  69. {

  70.        
  71.         public static void main(String[] args)
  72.         {
  73.                 TreeSet<StudentDemo> ts = new TreeSet<StudentDemo>();
  74.                 ts.add(new StudentDemo("zhangsan2",22,80));
  75.                 ts.add(new StudentDemo("zhangsan2",22,66));
  76.                 //ts.add(new StudentDemo("zhangsan1",20,90));
  77.                 //ts.add(new StudentDemo("zhangsan3",24,70));
  78.                 //ts.add(new StudentDemo("zhangsan4",19,95));
  79.                 //ts.add(new StudentDemo("zhangsan5",28,63));

  80.                
  81.                 Iterator it = ts.iterator();
  82.                 while(it.hasNext())
  83.                 {
  84.                         StudentDemo stu = (StudentDemo)it.next();
  85.                         System.out.println(stu.getName()+":"+stu.getAge()+"..."+stu.getScore());
  86.                 }
  87.         }
  88.        

  89. }






复制代码
还有一个问题,添加2个元素,分数不同,名字和年龄相同,2个都打印出来, 如何操作可以根据名字相同 第二个元素覆盖第一个元素?
回复 使用道具 举报
public int compareTo(StudentDemo s)

        {

                int num = new Integer(this.score).compareTo(new Integer(s.score));

               

                if(num == 0)

                {

                        return this.name.compareTo(s.name);

                }

                return num;

        }

回复 使用道具 举报
你的第二个问题把caompareTo函数写成下面的就行,只比较名字,相同名字只存入第一个人
public int compareTo(StudentDemo s)

        {
                        return this.name.compareTo(s.name);


        }

回复 使用道具 举报
郑洋洋 发表于 2012-3-26 20:32
public int compareTo(StudentDemo s)

        {

为什么是这一段呢?这一段不是一个比较排序的功能吗?
回复 使用道具 举报
郑洋洋 发表于 2012-3-26 20:37
你的第二个问题把caompareTo函数写成下面的就行,只比较名字,相同名字只存入第一个人
public int compare ...

public int compareTo(StudentDemo s)

        {
                        return this.name.compareTo(s.name);


        }

这个代码可以实现 名字 相同,后面来的元素存不进来 是吧?

如果有个需求是 后面来的 元素 和前面名字相同,不是存不进来,而是覆盖前面的呢?
回复 使用道具 举报
Set集合元素是不可以重复,判断重复的标准就是通过 CompareTo()方法的返回值如果是0,说明是相同元素,不可以存进去第二个
回复 使用道具 举报
可见 中级黑马 2012-3-26 21:39:57
8#
郑洋洋 发表于 2012-3-26 21:01
Set集合元素是不可以重复,判断重复的标准就是通过 CompareTo()方法的返回值如果是0,说明是相同元素,不可 ...

thx a lot...
回复 使用道具 举报
TreeSet使用Compare方法(自然排序或者构造集合传入比较器)保证唯一性并且进行排,HashSet使用hashCode方法和equals方法比较是否相同。
回复 使用道具 举报
谢解疑   thx a little
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马