Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A <code>push/pop</code> operation is clearly defined for a stack abstract data type; I'm not sure if it makes sense for a queue (or even a priority queue).</p> <p><a href="http://java.sun.com/javase/6/docs/api/java/util/PriorityQueue.html" rel="nofollow noreferrer"><code>PriorityQueue</code></a><code>implements</code><a href="http://java.sun.com/javase/6/docs/api/java/util/Queue.html" rel="nofollow noreferrer"><code>Queue</code></a>, which only specifies <code>add/remove</code>. On the other hand, a <a href="http://java.sun.com/javase/6/docs/api/java/util/Deque.html" rel="nofollow noreferrer"><code>Deque</code></a> has <code>addFirst/Last</code>, <code>removeFirst/Last</code>, etc. Perhaps one of these is what you're looking for.</p> <hr> <h3>An example</h3> <p>Here's an example of using a <code>PriorityQueue</code> of <code>String</code>, using a custom <a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow noreferrer"><code>Comparator</code></a> that compares lengths.</p> <pre><code> Queue&lt;String&gt; queue = new PriorityQueue&lt;String&gt;( 100, new Comparator&lt;String&gt;() { @Override public int compare(String s1, String s2) { return Integer.valueOf(s1.length()).compareTo(s2.length()); } } ); queue.add("Sally"); queue.add("Amy"); queue.add("Alice"); System.out.println(queue); // "[Amy, Sally, Alice]" System.out.println(queue.remove()); // "Amy" System.out.println(queue.remove()); // "Alice" queue.add("Tina"); System.out.println(queue.remove()); // "Tina" </code></pre> <p>As expected, the <code>PriorityQueue</code> will give the shortest <code>String</code> in the queue upon <code>remove</code>. Also as specified, ties are broken arbitrarily.</p> <h3>Related questions</h3> <p>On <code>PriorityQueue</code></p> <ul> <li><a href="https://stackoverflow.com/questions/683041/java-how-do-i-use-a-priorityqueue"> Java: How do I use a PriorityQueue? </a></li> <li><a href="https://stackoverflow.com/questions/627130/in-java-what-should-i-use-for-a-priorityqueue-that-returns-the-greatest-element-f">In Java what should I use for a PriorityQueue that returns the greatest element first?</a></li> </ul> <p>On <code>Comparator</code> and <code>Comparable</code></p> <ul> <li><a href="https://stackoverflow.com/questions/2266827/when-to-use-comparable-vs-comparator">When to use Comparable vs Comparator</a></li> <li><a href="https://stackoverflow.com/questions/1440134/java-what-is-the-difference-between-implementing-comparable-and-comparator">Java: What is the difference between implementing Comparable and Comparator?</a></li> <li><a href="https://stackoverflow.com/questions/420223/difference-between-compare-and-compareto">difference between compare() and compareTo()</a></li> <li><a href="https://stackoverflow.com/questions/2858628/comparable-and-comparator-contract-with-regards-to-null">Comparable and Comparator contract with regards to null</a></li> <li><a href="https://stackoverflow.com/questions/2892947/why-does-the-java-collections-framework-offer-two-different-ways-to-sort/">Why does the Java Collections Framework offer two different ways to sort?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload