如果自定义的类想要添加到TreeSet集合中就必须 实现 Comparable 或者 Comparator
2 创建一个类 去实现Comparator接口 并且重写compare方法
如果实现Comparator 就是 定制排序
定制排序的步骤
1 定义bean类2 创建一个类 去实现Comparator接口 并且重写compare方法
3 创建TreeSet对象 向构造方法中传递
public class TestNO2Student {
private int age;
private String Bname;
private String name;
private int price;
public TestNO2Student(int age, String bname, String name, int price) {
super();
this.age = age;
Bname = bname;
this.name = name;
this.price = price;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBname() {
return Bname;
}
public void setBname(String bname) {
Bname = bname;
}
@Override
public String toString() {
return "TestNO2Student [Bname=" + Bname + ", age=" + age + ", name="
+ name + ", price=" + price + "]";
}
}
public class TestNO2Comp implements Comparator<TestNO2Student> {
@Override
public int compare(TestNO2Student o1, TestNO2Student o2) {
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return 2;
} else {
CollationKey key = Collator.getInstance().getCollationKey(
o1.getName());
CollationKey key1 = Collator.getInstance().getCollationKey(
o2.getName());
if (key.compareTo(key1) > 0) {
return 1;
} else if (key.compareTo(key1) < 0) {
return -1;
} else {
if (o1.getPrice() > o2.getPrice()) {
return -1;
} else if (o1.getPrice() < o2.getPrice()) {
return 1;
} else {
CollationKey key3 = Collator.getInstance().getCollationKey(
o1.getBname());
CollationKey key4 = Collator.getInstance().getCollationKey(
o2.getBname());
return key3.compareTo(key4);
}
}
}
}
}
public class TestNo2 {
public static void main(String[] args) {
TreeSet<TestNO2Student> set = new TreeSet<TestNO2Student>(
new TestNO2Comp());
set.add(new TestNO2Student(11, "童年", "蒋", 22));
set.add(new TestNO2Student(13, "在人间", "蒋", 2));
set.add(new TestNO2Student(11, "我的大学", "刘", 22));
set.add(new TestNO2Student(21, "童年", "杨", 22));
System.out.println(set);
// 或者使用匿名类
TreeSet<TestNO2Student> set1 = new TreeSet<TestNO2Student>(
new Comparator<TestNO2Student>() {
@Override
public int compare(TestNO2Student o1, TestNO2Student o2) {
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return 2;
} else {
CollationKey key = Collator.getInstance()
.getCollationKey(o1.getName());
CollationKey key1 = Collator.getInstance()
.getCollationKey(o2.getName());
if (key.compareTo(key1) > 0) {
return 1;
} else if (key.compareTo(key1) < 0) {
return -1;
} else {
if (o1.getPrice() > o2.getPrice()) {
return -1;
} else if (o1.getPrice() < o2.getPrice()) {
return 1;
} else {
CollationKey key3 = Collator.getInstance()
.getCollationKey(o1.getBname());
CollationKey key4 = Collator.getInstance()
.getCollationKey(o2.getBname());
return key3.compareTo(key4);
}
}
}
}
});
set1.add(new TestNO2Student(11, "童年", "张", 22));
set1.add(new TestNO2Student(13, "在人间", "蒋", 26));
set1.add(new TestNO2Student(11, "我的大学", "", 22));
set1.add(new TestNO2Student(21, "童年", "蒋", 22));
System.out.println(set1);
}
}