title: Why does PriorityQueue peek() return the wrong maximum value?
body: <p>I'm using <code>PriorityQueue</code> 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. Second method tries to update the target item's big number; but when I tried <code>peek()</code> method after update method is called, it still prints out the older maximum number instead of the new one after update.</p>
  
  <p>Here is my code:</p>
  
  <pre><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);    
  }
  </code></pre>
  
  <p>EDITED: It seems like it only works if I removed the target item from allItems(Refer to EDITED in update method). Why is this so?</p>
  
  <pre><code>class Item implements Comparable&lt;Item&gt;{
          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 &gt; x) return 1;
              if (y &lt; x) return -1;
  
  
              if (this.arrived &gt; o.arrived) return 1;
              if (this.arrived &lt; o.arrived) return -1;
  
              return 0;
          }
  
      }
  </code></pre>