javacomparator,javacomparator原理

http://www.itjxue.com  2023-01-16 13:11  来源:未知  点击次数: 

Java中Comparator接口的用法

在java编程中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到目标,用法如下(模拟下在集合对象中对日期属性进行排序):

一、实体类Step

package?com.ljq.entity;

/**

?*?运号单流程

?*?

?*?@author?Administrator

?*?

?*/

public?class?Step{

????/**?处理时间?*/

????private?String?acceptTime?=?"";

????/**?快件所在地点?*/

????private?String?acceptAddress?=?"";

????public?Step()?{

????????super();

????}

????public?Step(String?acceptTime,?String?acceptAddress)?{

????????super();

????????this.acceptTime?=?acceptTime;

????????this.acceptAddress?=?acceptAddress;

????}

????public?String?getAcceptTime()?{

????????return?acceptTime;

????}

????public?void?setAcceptTime(String?acceptTime)?{

????????this.acceptTime?=?acceptTime;

????}

????public?String?getAcceptAddress()?{

????????return?acceptAddress;

????}

????public?void?setAcceptAddress(String?acceptAddress)?{

????????this.acceptAddress?=?acceptAddress;

????}

}

二、实现Comparator接口

package?com.ljq.entity;

import?java.util.Comparator;

import?java.util.Date;

import?com.ljq.util.UtilTool;

/**

?*?对Step类进行排序

?*?

?*?@author?Administrator

?*

?*/

public?class?StepComparator?implements?ComparatorStep{

????/**

?????*?如果o1小于o2,返回一个负数;如果o1大于o2,返回一个正数;如果他们相等,则返回0;

?????*/

????@Override

????public?int?compare(Step?o1,?Step?o2)?{

????????Date?acceptTime1=UtilTool.strToDate(o1.getAcceptTime(),?null);

????????Date?acceptTime2=UtilTool.strToDate(o2.getAcceptTime(),?null);

????????//对日期字段进行升序,如果欲降序可采用before方法

????????if(acceptTime1.after(acceptTime2))?return?1;

????????return?-1;

????}

}

三、代码测试

package?junit;

import?java.util.Collection;

import?java.util.Collections;

import?java.util.List;

import?org.junit.Test;

public?class?StepComparatorTest?{

????@Test

????public?void?sort()?throws?Exception{

????????ListStep?steps=new?ArrayListStep;

????????//对集合对象进行排序

?????????StepComparator?comparator=new?StepComparator();

????????Collections.sort(steps,?comparator);

????????if(steps!=nullsteps.size()0){

????????????for(Step?step:steps){

???????????????System.out.println(step.getAcceptAddress());

???????????????System.out.println(step.getAcceptTime());

????????????}

????????}

????}

}

java中Comparable和Comparator两种比较器的区别

Comparable和Comparator接口都是为了对类进行比较,众所周知,诸如Integer,double等基本数据类型,java可以对他们进行比较,而对于类的比较,需要人工定义比较用到的字段比较逻辑。可以把Comparable理解为内部比较器,而Comparator是外部比较器,基本的写法如下:

class Apple implements ComparableApple{

int id;

double price;

public Apple(int id, double price) {

this.id = id;

this.price = price;

}

public int compareTo(Apple o) {

//return Double.compare(this.getPrice(),o.getPrice());

if (Math.abs(this.price-o.price)0.001)

return 0;

else

return (o.price-this.price)0?1:-1;

}

@Override

public String toString() {

return "Apple{" +

"id=" + id +

", price=" + price +

'}';

}

}

class AESComparator implements ComparatorApple{

public int compare(Apple o1, Apple o2) {

if (Math.abs(o1.price-o2.price)0.001)

return 0;

else{

return (o1.price-o2.price)0?1:-1;

}

}

}

实现了Comparable接口的类需要实现compareTo()方法,传入一个外部参数进行比对,实现了Comparator接口的方法需要实现compare()方法,对外部传入的两个类进行比较,从而让外部方法在比较时调用。

两者的区别是实现Comparator接口代码更加灵活,可以定义某个类的多个比较器,从而在排序时根据实际场景自由调用,而Comparable接口实现后便不能改动。两种接口的调用方式如下:

class AESComparator implements ComparatorApple{

public int compare(Apple o1, Apple o2) {

if (Math.abs(o1.price-o2.price)0.001)

return 0;

else{

return (o1.price-o2.price)0?1:-1;

}

}

}

class DESComparator implements ComparatorApple{

public int compare(Apple o1, Apple o2) {

if (Math.abs(o1.price-o2.price)0.001)

return 0;

else {

return (o1.price-o2.price)0?-1:1;

}

}

}

public static void main(String[] args) {

Apple apple1 = new Apple(1,4.8);

Apple apple2 = new Apple(2,5.9);

Apple apple3 = new Apple(3,8.5);

ListApple list = new ArrayListApple();

list.add(apple1);

list.add(apple3);

list.add(apple2);

System.out.println("Comparable==========");

System.out.printf("this list of apples: %s\n",list);

Collections.sort(list);

System.out.printf("this list of apples: %s\n",list);

System.out.println("Comparator==========");

System.out.printf("this list of apples: %s\n",list);

Collections.sort(list,new DESComparator());

System.out.printf("this list of apples: %s\n",list);

Collections.sort(list,new AESComparator());

System.out.printf("this list of apples: %s\n",list);

}

}

上述代码存在的问题,不能在比较器中进行double类型的减法操作,因为对于值比较大的double,减法操作容易导致值的溢出,java7对每一种包装类型的比较新增了compare()方法,改造后的代码如下:

class Apple implements ComparableApple{

int id;

double price;

public Apple(int id, double price) {

this.id = id;

this.price = price;

}

public int compareTo(Apple o) {

return Double.compare(this.price,o.price);

}

@Override

public String toString() {

return "Apple{" +

"id=" + id +

", price=" + price +

'}';

}

}

class AESComparator implements ComparatorApple{

public int compare(Apple o1, Apple o2) {

return Double.compare(o1.price,o2.price);

}

}

class DESComparator implements ComparatorApple{

public int compare(Apple o1, Apple o2) {

return Double.compare(o2.price,o1.price);

}

}

查看Double.compare的源码如下

public static int compare(double d1, double d2) {

if (d1 d2)

return -1; // Neither val is NaN, thisVal is smaller

if (d1 d2)

return 1; // Neither val is NaN, thisVal is larger

// Cannot use doubleToRawLongBits because of possibility of NaNs.

long thisBits = Double.doubleToLongBits(d1);

long anotherBits = Double.doubleToLongBits(d2);

return (thisBits == anotherBits ? 0 : // Values are equal

(thisBits anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)

1)); // (0.0, -0.0) or (NaN, !NaN)

}

请教:JAVA中,comparator接口如何对double型数据进行排序。

返回值是int

-1,0或1。1表示第一个数比第二个数大,0表示相等,-1表示第一个数比第二个数小。这个int跟你要比较的数值类型没关系。举例:

import

java.util.Comparator;

public

class

Test1

implements

Comparator

{

public

int

compare(Double

o1,

Double

o2)

{

return

o1.compareTo(o2);

}

public

static

void

main(String[]

args)

{

Test1

t

=

new

Test1();

double

i=10,j=9;

System.out.println(t.compare(j,

i));

}

}

Java编写程序实现Comparator接口,实现字符串按 降序排序怎么弄?

Comparator升序降序实现 int compare(Object o1, Object o2) 返回一个基本类型的整型 1) 如果要按照升序排序, 则o1 小于o2,返回-1(负数),相等返回0,01大于02

java中Comparator接口,为什么o1.length()-o2.length()就是升序,o2.length() - o1.lengh()就是降序呢?

并不是因为o1.length() - o2.length() 就是升序,o2.length() - o1.lengh()就是降序,而是因为

你实现了用Comparator这个接口的时候,就有compare这个方法,JDK会在比较的时候自动调用这个方法,当该方法返回正数时,以第一个参数大于第二个,反之亦然。

这里只是用了2个String参数的长度来决定哪个比较大,你也可以自己来实现比较的逻辑。比如传入的2个参数的第一个字母比较之类的。javase的问题多看书。看帖子和问人得来的收获太小了。

建议啃完《thinking in java》

(责任编辑:IT教学网)

更多

推荐新手入门文章