import java.util.*;
class Person
{
private String name;
Person(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public String toString()
{
return "person:"+name;
}
}
class Student extends Person implements Comparable<Student>
{
Student(String name)
{
super(name);
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.getName().compareTo(o.getName());
}
}
class Worker extends Person
{
Worker(String name)
{
super(name);
}
}
class GenericDemo3
{
public static void main(String[] args)
{
//Student s = new Student();
TreeSet<Student> ts = new TreeSet<Student>();
ts.add(new Student("lisi01"));
ts.add(new Student("lisi02"));
ts.add(new Student("lisi03"));
Iterator<Student> it = ts.iterator();
while (it.hasNext())
{
System.out.println(it.next().getName());
}
/*Student s = new Student("lisi001");
System.out.println(s.getName());*/
}
}作者: 廖志强 时间: 2013-5-28 20:48
有两种方式,第一种同楼上的,用comparable
第二种可以用接口comparator比较器接口实现。如下
class GenericDemo3
{
public static void main(String[] args)
{
//Student s = new Student();
TreeSet<Student> tree = new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num = o1.getName().length()-o2.getName().length();
int num2=(num==0)?(o1.getAge()-o2.getAge()):num;
int num3=(num2==0)?o1.getName().compareTo(o2.getName()):num2;
return num3;
}
});
ts.add(new Student("lisi01"));
ts.add(new Student("lisi02"));
ts.add(new Student("lisi03"));
Iterator<Student> it = ts.iterator();
while (it.hasNext())
{
System.out.println(it.next().getName());
}
/*Student s = new Student("lisi001");
System.out.println(s.getName());*/
Iterator<Student> it = ts.iterator();
while (it.hasNext())
{
System.out.println(it.next().getName());
}
/*Student s = new Student("lisi001");
System.out.println(s.getName());*/
作者: 不胖的胖子 时间: 2013-5-28 21:48
1 先来看看错误
TreeSet<Student> ts = new TreeSet<Student>();
ts.add(new Student("lisi01"));
ts.add(new Student("lisi02"));//ClassCastException: Student cannot be cast to java.lang.Comparable
class Person implements Comparable//实现Compareable接口
{
private String name;
Person(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public String toString(){
return "person:"+name;
}
public int compareTo(Object o1){ //必须覆盖compareTo方法
return -1;
}
}
class Student extends Person{
Student(String name) {
super(name);
}
}
class GenericDemo3
{
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
ts.add(new Student("lisi01"));
ts.add(new Student("lisi02"));
ts.add(new Student("lisi03"));
Comparator com = ts.comparator();
System.out.println(com);//返回对象所用的比较器
Iterator<Student> it = ts.iterator();//返回迭代器
while (it.hasNext()) {
System.out.println(it.next().getName());//迭代集合元素
}
}
}
复制代码
解决方案是实现Compareable接口,实现compareTo方法。
但是这里要注意的是:
没有实现compareable接口时,怎么还是能添加一个元素 new Student("lisi01")?
原因很简单:
add 方法详解:
将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。更确切地讲,如果该 set 不包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则将指定元素 e 添加到此 set 中。如果此 set 已经包含这样的元素,则该调用不改变此 set 并返回 false。
因为刚开始的集合为空,添加第一个元素时,元素e2 为空引用,所以能添加进集合中。当再次添加时,add(new Student("lisi02")),调用完对象的equals方法后,会接着调用对象的compareTo方法决定第二个元素的位置。由于没有实现compareable接口,自然会出现上述类型转换异常。