package com.heilong.collection;
import java.util.Comparator;
import java.util.TreeSet;
class Emp1 implements Comparable<Emp1>{
int id;
String name;
int salary;
public Emp1(int id, String name, int salary){
super();
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "{ 编号:" + this.id + " 姓名:" + this.name + " 薪水:" + this.salary;
}
@Override
public int compareTo(Emp1 e) {
System.out.println(this.name + "cpmpare" + e.name);
return this.salary-e.salary;
}
}
class Emp2 {
int id;
String name;
int salary;
public Emp2(int id, String name, int salary){
super();
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "{ 编号:" + this.id + " 姓名:" + this.name + " 薪水:" + this.salary;
}
}
class MyComparator implements Comparator<Emp2>{
@Override
public int compare(Emp2 e1, Emp2 e2) {
return e1.salary-e2.salary;
}
}
public class treeSet {
public static void main(String[] args) {
TreeSet treeSet1 = new TreeSet();
treeSet1.add(new Emp1(001,"张三",200));
treeSet1.add(new Emp1(002,"李四",300));
treeSet1.add(new Emp1(003,"王五",100));
treeSet1.add(new Emp1(004,"赵六",500));
System.out.println(treeSet1);
System.out.println("******************************************");
MyComparator myComparator = new MyComparator();
TreeSet treeSet2 = new TreeSet(myComparator);
treeSet2.add(new Emp2(001,"张三",200));
treeSet2.add(new Emp2(002,"李四",300));
treeSet2.add(new Emp2(003,"王五",100));
treeSet2.add(new Emp2(004,"赵六",500));
System.out.println(treeSet2);
}
}
