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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 耿渊博 中级黑马   /  2014-4-2 10:28  /  1332 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 耿渊博 于 2014-4-2 21:43 编辑

用TreeSet添加对象去重复,怎么不对啊? 下面是代码
  1. package com.Iterator;
  2. import java.util.*;

  3. //定义学生类
  4. class Student implements Comparable{
  5.         String name;
  6.         int age;
  7.         Student(String name,int age ) {
  8.                 this.age = age;
  9.                 this.name = name;
  10.         }
  11.         public String getName() {
  12.                 return name;
  13.         }
  14.         public int getAge() {
  15.                 return age;
  16.         }
  17.         //复写compareTo
  18.         public int compareTo(Object obj){
  19.                 if(!(obj instanceof Student))
  20.                         throw new RuntimeException("不是学生对象");
  21.                 Student stu = (Student)obj;
  22.                
  23.                 if(this.age>stu.age)
  24.                         return 1;
  25.                 if(this.age == stu.age)
  26.                         return 0;
  27.                 return -1;
  28.         }
  29.         
  30.         
  31.         
  32. }
  33. public class TreeSetDemo {

  34.         public static void main(String[] args) {
  35.                 // TODO Auto-generated method stub
  36.                
  37.                 TreeSet ts = new TreeSet();
  38.                 //添加元素
  39.                 ts.add(new Student("lisi01",15));
  40.                 ts.add(new Student("lisi01",17));
  41.                 ts.add(new Student("lisi03",15));
  42.                 ts.add(new Student("lisi02",18));
  43.                
  44.                 //定义迭代器
  45.                 Iterator it = ts.iterator();
  46.                
  47.                 while(it.hasNext()){
  48.                         Student stu = (Student)it.next();
  49.                         sop(stu.getName()+"....."+stu.getAge());
  50.                 }
  51.                
  52.         }
  53.         public static void sop(Object obj){
  54.                 System.out.println(obj);
  55.         }

  56. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

5 个回复

倒序浏览
不在判断年龄相等之后,没做名字的判断,所以认为年龄相等,就为同一个人,所以添加错误了


  1.             if(this.age>stu.age)
  2.                         return 1;
  3.                 if(this.age == stu.age)
  4.                         if(this.name.equals(stu.name))
  5.                                 return 1;
  6.                                 else return -1;
  7.                 return -1;
复制代码

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
你定义的方法时,根据age来判定的,所以,他去重复也只会去除age相同的,
如果想去同名,就把判定改为name
回复 使用道具 举报
本帖最后由 Up↑Lee↗ 于 2014-4-2 10:52 编辑
  1. </blockquote></div><div class="blockcode"><blockquote>//package com.Iterator;
  2. import java.util.*;

  3. //定义学生类
  4. class Student implements Comparable{
  5.         String name;
  6.         int age;
  7.         Student(String name,int age ) {
  8.                 this.age = age;
  9.                 this.name = name;
  10.         }
  11.         public String getName() {
  12.                 return name;
  13.         }
  14.         public int getAge() {
  15.                 return age;
  16.         }
  17.         //复写compareTo
  18.         public int compareTo(Object obj){
  19.                 if(!(obj instanceof Student))
  20.                         throw new RuntimeException("不是学生对象");
  21.                 Student stu = (Student)obj;
  22.                
  23.                 if(this.age>stu.age)
  24.                         return 1;
  25.                 if(this.age == stu.age)
  26.                         //return 0;
  27.                                 return this.name.compareTo(stu.name);//记住,排序时,当主要条件相同时,一定判断一下次要条件。
  28.                 return -1;
  29.         }
  30.         
  31.         
  32.         
  33. }
  34. public class TreeSetDemo
  35.         {

  36.         public static void main(String[] args)
  37.                         {
  38.                 // TODO Auto-generated method stub
  39.                
  40.                 TreeSet ts = new TreeSet();
  41.                 //添加元素
  42.                 ts.add(new Student("lisi01",15));
  43.                 ts.add(new Student("lisi01",17));
  44.                 ts.add(new Student("lisi03",15));
  45.                 ts.add(new Student("lisi02",18));
  46.                
  47.                 //定义迭代器
  48.                 Iterator it = ts.iterator();
  49.                
  50.                 while(it.hasNext())
  51.                                 {
  52.                         Student stu = (Student)it.next();
  53.                         sop(stu.getName()+"....."+stu.getAge());
  54.                 }
  55.                
  56.         }
  57.         public static void sop(Object obj){
  58.                 System.out.println(obj);
  59.         }

  60. }
复制代码

解答都在代码上  你可以试试

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
如果你是想说,age相等就是重复。那么你的程序没有任何问题
这是运行结果
lisi01.....15
lisi01.....17
lisi02.....18
如果你想比较名字相同的是重复元素,那么你在比较器中没有对name进行比较。
回复 使用道具 举报
本帖最后由 syw02014 于 2014-4-2 15:01 编辑

给你写个简单的:
  1. public int compareTo(Object obj) {
  2.         if(!(obj instanceof Student))
  3.                 throw new ClassCastException("类型错误!!!");
  4.         Student stu=(Student)obj;
  5.         int num=this.name.compareTo(stu.name);//姓名相同按年龄排序
  6.         return num==0?this.age-stu.age:num;
  7. }
复制代码



回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马