import java.util.Comparator;
import java.util.TreeSet;
public class Comd {
public static void main(String [] args){
TreeSet<Fighting> aFightings=new TreeSet<Fighting>();
aFightings.add(new Fighting(1,"yubo"));
aFightings.add(new Fighting(2,"yubo"));
aFightings.add(new Fighting(3,"yubo"));
aFightings.add(new Fighting(4,"yubo"));
aFightings.add(new Fighting(5,"yubo"));
System.out.println(aFightings);
TreeSet<String> aSet=new TreeSet<>(new my());
aSet.add("1w");
aSet.add("1qqw");
aSet.add("1qqqqqw");
aSet.add("1q");
aSet.add("1www");
aSet.add("1eew");
System.out.println(aSet);
}
}
class my implements Comparator<String>{
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
if(o1.length()-o2.length()==0){
return o1.compareTo(o2);//根据字典序比较并返回一个值
}
return o1.length()-o2.length();
}
}
class Fighting implements Comparable<Fighting>{
private int age;
private String name;
public Fighting(){}
public Fighting(int age,String name){
this.age=age;
this.name=name;
}
public String toString(){
return this.age+this.name;
}
@Override
public int compareTo(Fighting o) {
//二叉排序 一切从根开始
int a=1;
if (this.age>o.age) {
//this.age 是第二数的属性
a=-1;
}
System.err.println(a);
// TODO Auto-generated method stub
return a;//返回正负决定顺序
}
}
二叉树 字典序 set 自然序
最新推荐文章于 2021-04-27 23:51:33 发布