Java PriorityQueue自定义Comparator

时间:2017-05-07 16:21:52

标签: java priority-queue

在我的PriorityQueue中,我有两种类型的客户,VIP和常客。我想首先服务VIP,然后定期服务。

如果CustomerID< 100它被认为是VIP。

如果客户是VIP,他会进入队列VIP部分的末尾

如果客户是常规的,他会在整个队列的末尾进行操作。

换句话说,我想按布尔VIP值排序,同时保留客户进入的顺序。

这是我的订单类

public class Order implements Comparable<Order> {
    private final int customerID;
    private final int amount;
    private final boolean vip_status;

    public Order(int customerID, int amount) { 
        this.customerID = customerID;
        this.amount = amount;
        this.vip_status = customerID < 100 ? true : false;

    }

    @Override
    public int compareTo(Order o) {
        if (vip_status && !o.vip_status) {
            return -1;
        }
        if (!vip_status && o.vip_status)
            return 1;
        return 0;
    }

    public int getCustomerID() {
        return customerID;
    }

    public int getAmount() {
        return amount;
    }

    public boolean isVip_status() {
        return vip_status;
    }
}

这是我填写队列的尝试:

import java.util.PriorityQueue;

public class MyPriorityQueue {
    public static void main(String[] args) {
        PriorityQueue<Order> queue = new PriorityQueue<>();
        Order o1 = new Order(1, 50);
        Order o2 = new Order(5, 30);
        Order o3 = new Order(4, 10);
        Order o4 = new Order(150, 5);
        Order o5 = new Order(2, 5);
        Order o6 = new Order(200, 5);

        queue.add(o1);
        queue.add(o2);
        queue.add(o3);
        queue.add(o4);
        queue.add(o5);
        queue.add(o6);

        while(!queue.isEmpty()){
            Order s = queue.poll();
            System.out.printf("VIP Status: %s CustomerID: %s Amount: %s%n", 
                        s.isVip_status(), s.getCustomerID(), s.getAmount());
        }
    }
}

结果我得到了(这是错误的):

VIP Status: true CustomerID: 1 Amount: 50
VIP Status: true CustomerID: 5 Amount: 30
VIP Status: true CustomerID: 2 Amount: 5
VIP Status: true CustomerID: 4 Amount: 10
VIP Status: false CustomerID: 150 Amount: 5
VIP Status: false CustomerID: 200 Amount: 5

这是我期望看到的(CustomerID 2和4应该按照它们的相同顺序):

VIP Status: true CustomerID: 1 Amount: 50
VIP Status: true CustomerID: 5 Amount: 30
VIP Status: true CustomerID: 4 Amount: 10
VIP Status: true CustomerID: 2 Amount: 5
VIP Status: false CustomerID: 150 Amount: 5
VIP Status: false CustomerID: 200 Amount: 5

更新:除了VIP之外,我不希望排序任何其他列。我不想添加“日期”,因为它感觉像是一个黑客,而不是理解Java如何工作。

2 个答案:

答案 0 :(得分:4)

看来,带有开箱即用的PriorityQueue类可以随意重新排序项目,如果它们相互比较

(这不是“java如何工作”,它只是代表Java Runtime附带的某个类的一点变态。)

所以,这可能有用:

  1. 引入新的OrderPlacement课程,其中包含a)Order和b)int priority

  2. PriorityQueue添加OrderPlacement个对象而不是Order个对象。

  3. 当您创建新的OrderPlacement对象时,请通过递增计数器为其发出新的priority

  4. 然后,您的OrderPlacement对象可以使用compareTo()方法,如下所示:

    @Override
    public int compareTo( OrderPlacement o ) 
    {
        int d = -Boolean.compare( order.vip_status, o.order.vip_status );
        if( d != 0 )
            return d;
        return Integer.compare( priority, o.priority );
    }
    

答案 1 :(得分:2)

如果您必须使用优先级队列执行此操作,则下面的代码将解决您的问题。请注意,我使用静态计数器来维护具有相同VIP状态的元素的正确顺序,因为相等元素在优先级队列中以随机顺序维护。这是因为优先级队列使用最小/最大堆数据结构,它只关心将min / max元素放在堆顶部而不关心相同元素的排序。

import java.util.PriorityQueue;

public class Order implements Comparable<Order> {
  private final int customerID;
  private final int amount;
  private final int vip_status;
  private final int score;
  private static int counter = 0;

  public Order(int customerID, int amount) {
    this.customerID = customerID;
    this.amount = amount;
    this.vip_status = customerID < 100 ? 0 : 1;
    this.score = counter++;
  }

  @Override
  public String toString() {
    return customerID + " : " + amount + " : " + vip_status;
  }

  @Override
  public int compareTo(Order o) {
    int status = ((Integer) this.vip_status).compareTo(o.vip_status);
    status = status == 0 ? ((Integer) this.score).compareTo(o.score) : status;
    return status;
  }

  public static void main(String[] args) {
    Order o1 = new Order(1000, 100);
    Order o2 = new Order(500, 100);
    Order o3 = new Order(99, 100);
    Order o4 = new Order(10, 100);
    Order o5 = new Order(200, 100);
    Order o6 = new Order(1, 100);

    PriorityQueue<Order> orderQueue = new PriorityQueue<>();
    orderQueue.offer(o1);
    orderQueue.offer(o2);
    orderQueue.offer(o3);
    orderQueue.offer(o4);
    orderQueue.offer(o5);
    orderQueue.offer(o6);

    System.out.println(orderQueue.poll());
    System.out.println(orderQueue.poll());
    System.out.println(orderQueue.poll());
    System.out.println(orderQueue.poll());
    System.out.println(orderQueue.poll());
    System.out.println(orderQueue.poll());
  }
}

`

示例输出:

99 : 100 : 0
10 : 100 : 0
1 : 100 : 0
1000 : 100 : 1
500 : 100 : 1
200 : 100 : 1

注意:您需要知道得分最终可能达到Integer.MAX_VALUE