TreeSet 的两种比较性
2015-08-11 16:37
405 查看
/*现在要用treeset
第一种比较性是让元素具有比较性
自定义的对象没有比较性,我们要让对象实现comparable 覆盖comparaTo
*/
//存储对象进入TREEset集合,并且里面有重覆元素
package pack;
import java.util.*;
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)
{
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet t=new TreeSet();
t.add(new Student("lisi",20));
t.add(new Student("lisi",20));
t.add(new Student("lisi1",20));
Iterator it=t.iterator();
while(it.hasNext())
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"...."+stu.getAge());
}
}
}
第一种比较性是让元素具有比较性
自定义的对象没有比较性,我们要让对象实现comparable 覆盖comparaTo
*/
//存储对象进入TREEset集合,并且里面有重覆元素
package pack;
import java.util.*;
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)
{
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet t=new TreeSet();
t.add(new Student("lisi",20));
t.add(new Student("lisi",20));
t.add(new Student("lisi1",20));
Iterator it=t.iterator();
while(it.hasNext())
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"...."+stu.getAge());
}
}
}
相关文章推荐
- 更新日志 - 关于 iOS9 设备的安装及其他优化
- Redis主从复制
- select服务端代码实现
- UIImageVIew
- 通过均匀分布随机数产生器获得一个高斯分布随机数产生器
- UIButton&UILabel
- Nightmare
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- xargs将stdin转换为命令行参数
- Android项目中如何用好构建神器Gradle?
- HTML语言学习网站
- 深入解析Oracle学习笔记(第五章)
- 反素数深度分析
- 24-IO流-64-IO流(操作数组的流)
- 24-IO流-65-IO流(编码表)
- fir.im Weekly - 我回来了
- Nginx实现负载均衡
- Android之通过VideoView控件播放一个视频出现的问题以及我的解决办法
- 合并单元格/VBA
- Diesel powered appear common making use of their very own model of elements