使用自定義Comparator對TreeSet中的數據進行多條件排序


代碼記錄(需求:根據店鋪等級和店鋪到某個點的距離進行排序,其中店鋪等級由高到低,距離由近及遠)

需要排序的對象Store,Store.java

package com.zhipengs.work.test;

import java.io.Serializable;

/**
 * 實體類或DTO
 * 
 * @author zhipengs
 */
public class Store implements Serializable {

    private static final long serialVersionUID = -1947476757586351017L;

    private double distance;// 店鋪到某個經緯度(點)的距離--距離某個固定的點越近,排序優先級越高
    private int sgrade;// 店鋪等級--等級越高,排序優先級越高

    public Store(double distance, int sgrade) {
        super();
        this.distance = distance;
        this.sgrade = sgrade;
    }

    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public int getSgrade() {
        return sgrade;
    }

    public void setSgrade(int sgrade) {
        this.sgrade = sgrade;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(distance);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + sgrade;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Store other = (Store) obj;
        if (Double.doubleToLongBits(distance) != Double
                .doubleToLongBits(other.distance))
            return false;
        if (sgrade != other.sgrade)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Store [distance=" + distance + ", sgrade=" + sgrade + "]";
    }

}

自定義Comparator,StoreComparator.java

package com.zhipengs.work.test;

import java.util.Comparator;

/**
 * 自定義StoreComparator,實現Comparator接口,重寫compare方法
 * 
 * @author zhipengs
 */
public class StoreComparator implements Comparator<Store> {

    @Override
    public int compare(Store o1, Store o2) {
        int ret = 0;
        // 店鋪等級由高到低
        int sg = o2.getSgrade() - o1.getSgrade();
        if (sg != 0) {
            ret = sg > 0 ? 1 : -1;
        } else {
            // 店鋪距離由近及遠
            sg = (o1.getDistance() - o2.getDistance()) > 0 ? 1 : -1;
            if (sg != 0) {
                ret = sg > 0 ? 1 : -1;
            }
        }
        return ret;
    }

}

測試類Main.java

package com.zhipengs.work.test;

import java.util.Set;
import java.util.TreeSet;

/**
 * 測試多條件排序TreeSet--Comparator
 * 
 * @author zhipengs
 */
public class Main {

    public static void main(String[] args) {
        // 先用TreeSet按自定義排序規則排序並控制size大小,再轉為有序List遍歷進行其它操作或處理
        Set<Store> storeSet = new TreeSet<Store>(new StoreComparator());
        storeSet.add(new Store(1, 0));
        storeSet.add(new Store(2, 1));
        storeSet.add(new Store(5, 1));
        storeSet.add(new Store(9, 2));
        storeSet.add(new Store(3, 0));
        storeSet.add(new Store(6, 0));
        storeSet.add(new Store(4, 1));
        storeSet.add(new Store(7, 2));
        storeSet.add(new Store(0, 0));
        storeSet.add(new Store(8, 1));
        int sgrade = -1;
        // 打印排序后的結果
        for (Store s : storeSet) {
            if (sgrade != s.getSgrade() && -1 != sgrade) {
                System.out.println("------------------------------");
            }
            System.out.println(s);
            sgrade = s.getSgrade();
        }
    }
}

 測試結果:

Store [distance=7.0, sgrade=2]
Store [distance=9.0, sgrade=2]
------------------------------
Store [distance=2.0, sgrade=1]
Store [distance=4.0, sgrade=1]
Store [distance=5.0, sgrade=1]
Store [distance=8.0, sgrade=1]
------------------------------
Store [distance=0.0, sgrade=0]
Store [distance=1.0, sgrade=0]
Store [distance=3.0, sgrade=0]
Store [distance=6.0, sgrade=0]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM