import java.util.*;
class Treeset
{
public static void main(String[] args)
{
TreeSet t = new TreeSet();
t.add(new student("a1",15));
t.add(new student("a2",15));
t.add(new student("a1",15));
t.add(new student("a3",16));
t.add(new student("a3",18));
for(Iterator it = t.iterator();it.hasNext();)
{
student tt = (student)it.next();//强制转成学生类型
sop(tt.getName()+","+tt.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class student implements Comparable//接口让学生具有比较性
{
private String name;
private int age;
student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)
{
if(!(obj instanceof student))
throw new RuntimeException("不是学生");
student t = (student)obj;
if(this.age > t.age)
return 1;
if(this.age==t.age)
return this.name.compareTo(t.name);//如果年龄相同,在比较姓名排序
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有