title: peek() method in Queue interface
body: <p>From <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html" rel="nofollow noreferrer">Java documentation</a>:</p>
  <blockquote>
  <p>The remove() and poll() methods remove and return the head of the queue.</p>
  <p>The element() and peek() methods return, but do not remove, the head of the queue.</p>
  </blockquote>
  <p>From the second point it says method <code>peek()</code> returns head element of queue then how come its not returning head element of queue in the following program?</p>
  <pre><code>public class PQ {
  
  public static void main(String[] args) {
      PriorityQueue&lt;String&gt; pq = new PriorityQueue&lt;String&gt;();
      pq.add(&quot;carrot&quot;); 
      pq.add(&quot;apple&quot;); 
      pq.add(&quot;banana&quot;);
      System.out.println(pq.poll() + &quot;:&quot; + pq.peek()); // prints apple and banana rather than apple and apple
  }
  }
  </code></pre>
  <p>Once the first element(carrot) is removed, apple becomes the head of queue(according to FIFO in queue) so peek()method should return apple right?</p>
  <p>Example2:</p>
  <pre><code>public class PQ {
  
  public static void main(String[] args) {
      PriorityQueue&lt;String&gt; pq = new PriorityQueue&lt;String&gt;();
      pq.add(&quot;carrot&quot;); 
      pq.add(&quot;apple&quot;); 
      pq.add(&quot;banana&quot;);
      System.out.println(pq); // prints [apple, carrot, banana] -&gt; it should be [apple, banana, carrot] right? if it is following natural sorting order
  }
  }
  </code></pre>