比较器

Java 集合框架提供了一套性能优良,使用方便的接口和类,java集合框架位于java.util包中, 所以当使用集合框架的时候需要进行导包。

Comparable(内部排序) int compareTo(Object obj);返回值为int,默认升序排序,自然排序,可以直接通过Collection.sort()或者Arrays.sort进行排序
public int compareTo(T obj);Comparable是排序接口,若一个类实现了Comparable接口,意味着该类支持排序,是一个内部比较器(自己去和别人比)。Comparable在要比较的对象的类中实现,可以比较该对象和另一个对象。

返回值的三种情况:
1.正数:当前对象大于目标对象
2.0
3.负数

 public class Card implements Comparable<Card>
{
	char suit;
	char rank;
	double randomValue;
	
	Card(char aSuit,char aRank)
	{
		suit=aSuit;
		rank=aRank;
		randomValue=Math.random();
	}
	
	void display()
	{		
		System.out.println(Character.toString(suit)+Character.toString(rank));
	}

	public int compareTo(Card c) 
	{
		return this.randomValue>c.randomValue? 1:(this.randomValue<c.randomValue?-1:0);
	}
}
import java.util.*;

public class Sort 
{
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		LinkedList<Card> cardList= new LinkedList<Card>();
		char[] valueArray={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
		char[] suitArray={'红','梅','方','黑'};
		
		for(int i=0;i<4;i++)
			for(int j=0;j<13;j++)
			{
				cardList.add(new Card(suitArray[i],valueArray[j]));
			}
		cardList.add(new Card('小','王'));
		cardList.add(new Card('大','王'));
		
		Collections.sort(cardList);
		
		for(Card c: cardList)
		{
			c.display();
		}
	}
}

将会按照randomValue的值顺序排列好输出

Comparator(外部排序) int compare(Object ob1,Object obj2);返回值为int,排序灵活,用户自定义想要的排序方法。 Comparator接口是比较器接口,类本身不支持排序,专门由若干个第三方的比较器(实现了Comparator接口的类)来进行类的排序,是一个外部比较器(策略模式)。Comparator在一个单独的类中实现,可以比较任意两个对象。

import java.util.*;

public class ComparatorRank implements Comparator<Card>
{
	public int compare(Card c1,Card c2)
	{
		return c1.rank>c2.rank? 1: (c1.rank<c2.rank? -1:0);
	}
}
Collections.sort(cardList,new ComparatorRank());
		for(Card c: cardList)
		{
			c.display();
		}

将会按照rank的排序进行输出

实例2:

import java.util.*;

public class ComparatorX implements Comparator<Point> {
	
	public int compare(Point p1,Point p2)
	{
		return p1.X>p2.X? 1: (p1.X<p2.X? -1:0);
	}
}
class ComparatorY implements Comparator<Point> {
	
	public int compare(Point p1,Point p2)
	{
		return p1.Y>p2.Y? 1: (p1.Y<p2.Y? -1:0);
	}
}
public class Point implements Comparable<Point> 
{
	public float X;
	public float Y;
public Point()
	{}
	public Point(float x,float y)
	{
		X=x;
		Y=y;
	}
	public void diplay()
	{
		System.out.println("X:"+X+"Y:"+Y);
	}
	public int compareTo(Point p)
	{
		float temp=(float)Math.sqrt(X*X+Y*Y);
		float tempP=(float)Math.sqrt(p.X*p.X+p.Y*p.Y);
		return temp>tempP? 1:(temp<tempP?-1:0);
	}

}
import java.util.*;

public class Run {

	public static void main(String[] args) {
 		
		List<Point> points=new ArrayList<Point>();
		points.add(new Point(1,1));//=1.4.....
		points.add(new Point(-1,2));//=2......
		points.add(new Point(2,0));//=2
		
		Collections.sort(points);		
		
		System.out.println(points.get(0).X);
		System.out.println(points.get(1).X);
		System.out.println(points.get(2).X);
		
		System.out.println("----------------------------");
		
		
		Collections.sort(points, new ComparatorX());
		System.out.println(points.get(0).X);
		System.out.println(points.get(1).X);
		System.out.println(points.get(2).X);
		
		System.out.println("----------------------------");
		
		Collections.sort(points, new ComparatorY());
		System.out.println(points.get(0).X);
		System.out.println(points.get(1).X);
		System.out.println(points.get(2).X);
	}

}

Collections.sort()放一个参数与两个参数有两种实现方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值