黑马程序员技术交流社区
标题:
用TreeSet添加对象去重复
[打印本页]
作者:
耿渊博
时间:
2014-4-2 10:28
标题:
用TreeSet添加对象去重复
本帖最后由 耿渊博 于 2014-4-2 21:43 编辑
用TreeSet添加对象去重复,怎么不对啊? 下面是代码
package com.Iterator;
import java.util.*;
//定义学生类
class Student implements Comparable{
String name;
int age;
Student(String name,int age ) {
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
//复写compareTo
public int compareTo(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student stu = (Student)obj;
if(this.age>stu.age)
return 1;
if(this.age == stu.age)
return 0;
return -1;
}
}
public class TreeSetDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet ts = new TreeSet();
//添加元素
ts.add(new Student("lisi01",15));
ts.add(new Student("lisi01",17));
ts.add(new Student("lisi03",15));
ts.add(new Student("lisi02",18));
//定义迭代器
Iterator it = ts.iterator();
while(it.hasNext()){
Student stu = (Student)it.next();
sop(stu.getName()+"....."+stu.getAge());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
复制代码
作者:
Aimer_WJY
时间:
2014-4-2 10:44
不在判断年龄相等之后,没做名字的判断,所以认为年龄相等,就为同一个人,所以添加错误了
if(this.age>stu.age)
return 1;
if(this.age == stu.age)
if(this.name.equals(stu.name))
return 1;
else return -1;
return -1;
复制代码
作者:
闲人
时间:
2014-4-2 10:46
你定义的方法时,根据age来判定的,所以,他去重复也只会去除age相同的,
如果想去同名,就把判定改为name
作者:
Up↑Lee↗
时间:
2014-4-2 10:50
本帖最后由 Up↑Lee↗ 于 2014-4-2 10:52 编辑
</blockquote></div><div class="blockcode"><blockquote>//package com.Iterator;
import java.util.*;
//定义学生类
class Student implements Comparable{
String name;
int age;
Student(String name,int age ) {
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
//复写compareTo
public int compareTo(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student stu = (Student)obj;
if(this.age>stu.age)
return 1;
if(this.age == stu.age)
//return 0;
return this.name.compareTo(stu.name);//记住,排序时,当主要条件相同时,一定判断一下次要条件。
return -1;
}
}
public class TreeSetDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
TreeSet ts = new TreeSet();
//添加元素
ts.add(new Student("lisi01",15));
ts.add(new Student("lisi01",17));
ts.add(new Student("lisi03",15));
ts.add(new Student("lisi02",18));
//定义迭代器
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
sop(stu.getName()+"....."+stu.getAge());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
复制代码
解答都在代码上 你可以试试
作者:
青木郎
时间:
2014-4-2 14:22
如果你是想说,age相等就是重复。那么你的程序没有任何问题
这是运行结果
lisi01.....15
lisi01.....17
lisi02.....18
如果你想比较名字相同的是重复元素,那么你在比较器中没有对name进行比较。
作者:
syw02014
时间:
2014-4-2 15:00
本帖最后由 syw02014 于 2014-4-2 15:01 编辑
给你写个简单的:
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new ClassCastException("类型错误!!!");
Student stu=(Student)obj;
int num=this.name.compareTo(stu.name);//姓名相同按年龄排序
return num==0?this.age-stu.age:num;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2