import java.util.TreeSet;
class Emp implements Comparable<Emp> {
String name;
int age;
int salary;
public Emp(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "{ 姓名:"+ this.name+" 年龄:"+ this.age+ " 薪水:"+this.salary+"}";
}
/*
* 元素与元素之间的比较规则定义在COmparaTo方法上。(non-Javadoc)
返回值:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
*/
@Override // 100.52 - 100.32 = 0.2
public int compareTo(Emp e) {
return this.salary-e.salary;
}
}
public class Demo111 {
public static void main(String[] args) {
TreeSet tree = new TreeSet();
tree.add(new Emp("家宝",68,200));
tree.add(new Emp("永康",78,100));
tree.add(new Emp("习总",58,300));
tree.add(new Emp("克强",48,500));
System.out.println(tree);
}
}
JAVA小练习111——TreeSet的练习
猜你喜欢
转载自blog.csdn.net/Eric_The_Red/article/details/91442630
今日推荐
周排行