简体   繁体   English

为什么PriorityQueue peek()返回错误的最大值?

[英]Why does PriorityQueue peek() return the wrong maximum value?

I'm using PriorityQueue to get the maximum integer, but I could not figure out why the first method returns the correct maximum integer but the second method doesn't. 我正在使用PriorityQueue来获取最大整数,但是我不知道为什么第一种方法返回正确的最大整数,而第二种方法却没有。 Second method tries to update the target item's big number; 第二种方法尝试更新目标商品的大数; but when I tried peek() method after update method is called, it still prints out the older maximum number instead of the new one after update. 但是当我在调用update方法后尝试使用peek()方法时,它仍然会打印出较旧的最大数字,而不是更新后的新数字。

Here is my code: 这是我的代码:

void add(String name, int number) {
    Item item = new Item(name, number, allItems.size());
    allItems.add(item);
    hashMap.put(name, item);
}

void update(String name, int number) {
    Item item = hashMap.get(name);

    // EDITED: 
    allItems.remove(item);

    item.setNumber(item.getNumber() + number);
    allItems.add(item);
    hashMap.put(name, item);    
}

EDITED: It seems like it only works if I removed the target item from allItems(Refer to EDITED in update method). 编辑:似乎只有在我从allItems中删除目标项目后,它才有效(请参考update方法中的EDITED)。 Why is this so? 为什么会这样呢?

class Item implements Comparable<Item>{
        private String name;
        private int number;
        private int arrived;

        public Item(String name, int number, int arrived) {
            this.name = name;
            this.number = number;
            this.arrived = arrived;
        }

        @Override
        public int compareTo(Item o) {

            int x = this.getNumber();
            int y = o.getNumber();

            if (y > x) return 1;
            if (y < x) return -1;


            if (this.arrived > o.arrived) return 1;
            if (this.arrived < o.arrived) return -1;

            return 0;
        }

    }

Most likely, your implementation of Comparable is incorrect. 很可能您的Comparable实现不正确。

Your Comparable implementation is what PriorityQueue uses to know how to sort the items based on the value of a field. 您的Comparable实现是PriorityQueue用来了解如何基于字段值对项目进行排序的工具。 You must implement Comparable in DECREASING order. 您必须以DECREASING顺序实现Comparable You want it to return -1 if the other item is bigger, and return 1 if this item is bigger; 你希望它返回-1 ,如果其他产品更大,并返回1 ,如果这个项目是更大; in your code you are doing the opposite. 在您的代码中,您做相反的事情。 In other words, make sure your Item class definition looks like this: 换句话说,请确保您的Item类定义如下所示:

public class Item implements Comparable<Item> {
  private int number;
  private int arrived;

  // the rest of your code

  public int compareTo(Item other) {
    int numberCompare = Integer.compare(other.number, this.number);
    if (numberCompare == 0) {
      return Integer.compare(other.arrived, this.arrived);
    } else {
      return numberCompare;
    }
  }
}

You can also use PriorityQueue(int initialCapacity, Comparator comparator) if you don't want to change the class definition of Item . 如果您不想更改Item的类定义,也可以使用PriorityQueue(int initialCapacity, Comparator comparator)

According to the Javadoc for PriorityQueue : 根据Javadoc的PriorityQueue

An unbounded priority queue based on a priority heap. 基于优先级堆的无界优先级队列。 The elements of the priority queue are ordered according to their natural ordering , or by a Comparator provided at queue construction time, depending on which constructor is used. 优先级队列的元素根据其自然顺序进行排序 ,或者由队列构造时提供的Comparator 排序 ,具体取决于所使用的构造函数。 A priority queue does not permit null elements. 优先级队列不允许null元素。 A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException ). 依赖自然顺序的优先级队列也不允许插入不可比较的对象(这样做可能会导致ClassCastException )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM