treeSet 对于对象的有序添加

package com.conpany.treeset;


import java.util.TreeSet;


//tree 能够按照自然顺序排序
//tree set 在对于无自然顺序的元素排序时要实现 comparable 接口


class Emp implements Comparable{
String name;
int id;
int salary;

public Emp(String name, int id, int salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
@Override
public String toString() {
return "{名字:"+this.name+"号码:"+this.id+"薪水:"+this.salary+"}";
}
@Override
public int compareTo(Object o) {
// 返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。 
Emp emp=(Emp)o;
return this.salary-emp.salary;
}
}
public class Demo1 {


public static void main(String[] args) {
TreeSet tree = new TreeSet();
// tree.add("c");
// tree.add("a");
// tree.add("b");
// System.out.println(tree);
tree.add(new Emp("老四",120,234));
tree.add(new Emp("老大",110,314));
tree.add(new Emp("老二",130,231));
tree.add(new Emp("老三",150,111));
System.out.println(tree);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值