本文主要是介绍Comparable 接口以及Comparator接口的简单使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
进行排序操作必须指定排序规则。 Arrays 的 sort()方法默认情况下对基本数据类型进行 排序,因为基本类型已经 指定了排序规则,而如果想对
对象类型进行排序,则该类要实现Comparable接口来确定排序规则。
Comparable 接口用来制定两个同类对象比较的规则。
Comparable接口定义:
public interface Comparable<T>{
public int compareTo( T o);
}
Comparable 接口采用了泛型,该接口中只有一个方法compareTo(T o), 该方法返回一个int 类型的数据,如果是1 表示大于关系,如果是-1 表示小于关系
,如果是0 表示等于关系。
示例:
package com.demo;import java.util.Arrays;class EnergyConsumption implements Comparable<EnergyConsumption>{public static String standtard = "万吨标准煤";private String department;private double consumption;public EnergyConsumption(String department, double consumption){this.department = department;this.consumption = consumption;}@Overridepublic int compareTo(EnergyConsumption ec) { //覆写比较规则,按照从小到大排序if(this.consumption>ec.consumption)return 1; //如果将此改为return -1else if(this.consumption < ec.consumption)return -1; //将此改为return 1, 将会从大到小排序elsereturn 0;}public String toString(){return "\n" + department + "," + consumption;}
}
public class demo {public static void main(String[] args) {EnergyConsumption ec1 = new EnergyConsumption("农业", 7971.53); EnergyConsumption ec2 = new EnergyConsumption("煤炭", 6916.95);EnergyConsumption ec3 = new EnergyConsumption("石油", 8343.34);EnergyConsumption[] ec = {ec1, ec2, ec3};System.out.println("打印排序之前的情况");System.out.println(Arrays.toString(ec));Arrays.sort(ec);System.out.println("打印排序之后的情况");System.out.println(Arrays.toString(ec));}}
输出结果:
打印排序之前的情况
[
农业,7971.53,
煤炭,6916.95,
石油,8343.34]
打印排序之后的情况
[
煤炭,6916.95,
农业,7971.53,
石油,8343.34]
在利用Arrays.sort() 方法排序时,所有对象都将向Comparable接口转换,基本数据类型和Sting类型已经实现了该接口,所以可以直接对它们排序。
‘
Comparator接口
如果在开发类时没有实现Comparable接口,但是在类的应用中需要对该类的对象进行排序,这种情况下利用java.util包中的Comparator接口作为一种
补救方法。Comparator接口用来实现两个对象的比较,该接口只有一个方法 compare(T o1, T o2)。在进行排序时则需要制定该接口的实例作为比较器。
Comparator接口定义:
public interface Comparator<T>{public int compare(T o1, T o2);
}
示例:
package com.demo;import java.util.Arrays;
import java.util.Comparator;class EnergyConsumption {public static String standtard = "万吨标准煤";private String department;private double consumption;public EnergyConsumption(String department, double consumption){this.department = department;this.consumption = consumption;}public double getConsumption(){return consumption;}public String toString(){return "\n" + department + "," + consumption;}
}
/** 定义比较器,实现comparator接口*/
class EnergyConsumptionComparator implements Comparator<EnergyConsumption>{@Overridepublic int compare(EnergyConsumption o1, EnergyConsumption o2) {if(o1 == o2)return 0;else if(o1.getConsumption() > o2.getConsumption())return 1;else if(o1.getConsumption() < o2.getConsumption())return -1;elsereturn 0;}}
public class demo {public static void main(String[] args) {EnergyConsumption ec1 = new EnergyConsumption("农业", 7971.53); EnergyConsumption ec2 = new EnergyConsumption("煤炭", 6916.95);EnergyConsumption ec3 = new EnergyConsumption("石油", 8343.34);EnergyConsumption[] ec = {ec1, ec2, ec3};System.out.println("打印排序之前的情况");System.out.println(Arrays.toString(ec));//排序时指明排序器,排序器是Comparator的一个对象Arrays.sort(ec, new EnergyConsumptionComparator());System.out.println("打印排序之后的情况");System.out.println(Arrays.toString(ec));}}
输出结果:
打印排序之前的情况
[
农业,7971.53,
煤炭,6916.95,
石油,8343.34]
打印排序之后的情况
[
煤炭,6916.95,
农业,7971.53,
石油,8343.34]
这篇关于Comparable 接口以及Comparator接口的简单使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!