zoukankan      html  css  js  c++  java
  • 使用自定义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]

  • 相关阅读:
    ES6-Generator
    ES6-Iterator & for...of循环
    ES6-Proxy and Reflect
    在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)
    函数的调用(取两个整型变量中的最大值)
    将一个字符串数组的元素的顺序进行翻转。。
    枚举类型练习
    利用Arraylist输入学生的成绩,求出平均分和总分。
    简单的推箱子游戏(利用数组)
    枚举类型的声明
  • 原文地址:https://www.cnblogs.com/once/p/3622931.html
Copyright © 2011-2022 走看看