Comparable 接口学习:对对象List进行比较和排序(正序和逆序)

Comparable 接口只有一个 int compareTo(T o) 方法

1、int compareTo(T o) 方法

  • 方法说明:
    • 比较此对象和规定的对象,如果此对象大于,等于,小于规定对象,则分别返回负整数,0和正整数。
  • 代码说明: (T)a.compareTo(T b)
    • a>b:返回一个正整数
    • a=b:返回0。
    • a<b:返回一个负整数

2、通俗点来说:

  • compareTo(T o) 方法实际定义了一套比较规则,规则就是方法体。
  • 比较结果就是方法返回结果。
    • 返回正整数表示当前对象大于比较的对象;
    • 返回负整数表示当前对象小于比较的对象;
    • 返回0表示当前对象等于比较的对象;
  • 所有要比较的对象都可以继承 Comparable 接口并实现 compareTo(T o) 方法来制定自己的比较规则。

3、根据比较规则和比较结果进行排序(正序、逆序)

  • 排序,要么从大到小,要么从小到大。没有其他了。
  • 而大小的判断,则是通过compareTo(T o) 方法的返回来判断的。
  • 所以,排序也有了。

4、上面都是理论说明,具体,怎么应用到对象上呢?

5、我的实现

public class DataStatisticVo1 implements Comparable <DataStatisticVo1> {
  
  //日汇总页面使用
  private String businessCode;
  private String created;
  private String vendorId;
  private String denomination;

  public String getBusinessCode() {
    return businessCode;
  }

  public void setBusinessCode(String businessCode) {
    this.businessCode = businessCode;
  }

  public String getCreated() {
    return created;
  }

  public void setCreated(String created) {
    this.created = created;
  }

  public String getVendorId() {
    return vendorId;
  }

  public void setVendorId(String vendorId) {
    this.vendorId = vendorId;
  }

  public String getDenomination() {
    return denomination;
  }

  public void setDenomination(String denomination) {
    this.denomination = denomination;
  }

  @Override
  public int compareTo(DataStatisticVo1 another) {
    //正常的比较,大于时返回1表示正序;大于时返回0表示逆序
    //创建时间逆序
    if(null!=this.getCreated()&&null!=another.getCreated()){
        //比较结果>0,表示this比another大,但为例实现逆序,我们需要定义返回-1,表示this比another小
      if(this.getCreated().compareTo(another.getCreated())>0){
        return -1;
      }
      if (this.getCreated().compareTo(another.getCreated())<0){
        return 1;
      }
    }

    if(null!=this.getVendorId()&&null!=another.getVendorId()){
      //商家ID正序
      if(Long.parseLong(this.getVendorId())>Long.parseLong(another.getVendorId())){
        return 1;
      }
      if(Long.parseLong(this.getVendorId())<Long.parseLong(another.getVendorId())){
        return -1;
      }
    }

    if(null!=this.getBusinessCode()&&null!=another.getBusinessCode()){
      //业务类型正序
      if(this.getBusinessCode().compareTo(another.getBusinessCode())>0){
        return 1;
      }
      if (this.getBusinessCode().compareTo(another.getBusinessCode())<0){
        return -1;
      }
    }

    if(null!=this.getDenomination()&&null!=another.getDenomination()){
      //面额单位逆序
      String unitThis = this.getDenomination().substring(this.getDenomination().length()-1,this.getDenomination().length());
      String unitAnother = another.getDenomination().substring(another.getDenomination().length()-1,another.getDenomination().length());
      if(unitThis.compareTo(unitAnother)>0){
        return -1;
      }
      if (unitThis.compareTo(unitAnother)<0){
        return 1;
      }
      if(unitThis.equals(unitAnother)){
        //面额数值逆序
        if(Long.parseLong(this.getDenomination().substring(0,(this.getDenomination().length()-1)))>Long.parseLong(another.getDenomination().substring(0,(another.getDenomination().length()-1)))){
          return -1;
        }
        if(Long.parseLong(this.getDenomination().substring(0,(this.getDenomination().length()-1)))<Long.parseLong(another.getDenomination().substring(0,(another.getDenomination().length()-1)))){
          return 1;
        }
      }
    }
    //0表示相等
    return 0;
  }
}
原文地址:https://www.cnblogs.com/buwuliao/p/11063239.html