在做软件构造lab3实验的时候,为了想让时间段,按照起始时间的大小从低到高排序,尝试了比较器。
public class timeBlock implements Comparator{
private long begin;
private long end;
public timeBlock(long begin, long end){
this.begin=begin;
this.end=end;
}
public long getBegin() {
return this.begin;
}
public long getEnd() {
return this.end;
}
@Override
public String toString() {
return "("+this.begin+"->"+this.end+")";
}
@Override
public int compare(Object o1, Object o2) {
if(((timeBlock)o1).getBegin()>((timeBlock)o2).getBegin())
return 1;
else if (((timeBlock)o1).getBegin()<((timeBlock)o2).getBegin())
return -1;
else
return 0;
}
}
public class Main2 {
Set<String> set=new HashSet<>();
public static void main(String[] args) {
List<timeBlock> set=new ArrayList<>();
timeBlock a=new timeBlock(4,10);
timeBlock b=new timeBlock(2,15);
timeBlock c=new timeBlock(15,19);
set.add(a);
set.add(b);
set.add(c);
set.sort(Comparator.naturalOrder());
System.out.println(set);
}
}
但是发现sort处出现了这样的错误信息。
后阅读Comparator.naturalOrder()的spec,发现要求自定义类必须实现Comparable的接口才能使用Comparator.naturalOrder()来进行自然排序,同理Comparator.reverseOrder()也是一样。
于是对代码修改以实现Comparable
public class timeBlock2 implements Comparable<Object>{
private long begin;
private long end;
//20210622151943
/**
* for build a timeBlock
* @param begin positive
* @param end should be larger than begin
*/
public timeBlock2(long begin, long end){
this.begin=begin;
this.end=end;
}
public long getBegin() {
return this.begin;
}
public long getEnd() {
return this.end;
}
@Override
public String toString() {
return "("+this.begin+"->"+this.end+")";
}
@Override
public int compareTo(Object o) {
if(this.begin<((timeBlock2)o).getBegin())
return -1;
else if(this.begin>((timeBlock2)o).getBegin())
return 1;
else if(this.end<((timeBlock2)o).getEnd())
return -1;
else
return 0;
}
}
public class Main2 {
Set<String> set=new HashSet<>();
public static void main(String[] args) {
List<timeBlock2> set=new ArrayList<>();
timeBlock2 a=new timeBlock2(4,10);
timeBlock2 b=new timeBlock2(2,15);
timeBlock2 c=new timeBlock2(15,19);
set.add(a);
set.add(b);
set.add(c);
set.sort(Comparator.naturalOrder());
System.out.println(set);
}
}
同理在sort中传入参数Comparator.reverseOrder()也可以实现逆向排序。
实际上ArrayList.sort传入的那个参数是一个比较器,并利用compare函数进行sort。所以我们也可以通过自己写compare函数来实现想要的比较方法,例如想进行逆向排序,就直接向sort传一个参数,要求他实现Comparator接口,并Ovrride compare方法使得反向排序即可
public class timeBlock implements Comparator{
private long begin;
private long end;
public timeBlock(long begin, long end){
this.begin=begin;
this.end=end;
}
public long getBegin() {
return this.begin;
}
public long getEnd() {
return this.end;
}
@Override
public String toString() {
return "("+this.begin+"->"+this.end+")";
}
@Override
public int compare(Object o1, Object o2) {
if(((timeBlock)o1).getBegin()>((timeBlock)o2).getBegin())
return -1;
else if (((timeBlock)o1).getBegin()<((timeBlock)o2).getBegin())
return 1;
else
return 0;
}
}
public class Main2 {
Set<String> set=new HashSet<>();
public static void main(String[] args) {
List<timeBlock> set=new ArrayList<>();
timeBlock a=new timeBlock(4,10);
timeBlock b=new timeBlock(2,15);
timeBlock c=new timeBlock(15,19);
set.add(a);
set.add(b);
set.add(c);
set.sort(new timeBlock(0,1));
System.out.println(set);
}
}