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);
}
}
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);
}
}