软件构造(三)JAVA中 ArrayList对自定义类的自然排序

本文介绍了在Java编程中如何为时间块类实现排序。首先尝试使用Comparator.naturalOrder(),但遇到错误,因为自定义类需要实现Comparable接口。接着,修改代码使timeBlock2类实现Comparable接口,成功进行自然排序。此外,还展示了如何通过自定义Comparator实现逆向排序。
摘要由CSDN通过智能技术生成

在做软件构造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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值