TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java PriorityQueue Example (add, peek and poll)

Use the PriorityQueue class to add elements into a queue and poll them. Peek returns the first element.
PriorityQueue. This collection keeps all elements in a sorted order. Integers are kept ordered from low to high. Strings are kept ordered from first alphabetically to last.
We add elements, and they are stored in the PriorityQueue. Then we use peek() to see the first element in the queue. We use poll() to get and remove the first element.
First example. Here we create a PriorityQueue and add 2 strings to it. The PriorityQueue is generic so we must specify a type for its elements.

For: We use the for-loop to display all the elements in the queue. Notice how it goes from first-sorted to last (bird comes before cat).

Java program that uses PriorityQueue import java.util.PriorityQueue; public class Program { public static void main(String[] args) { PriorityQueue<String> queue = new PriorityQueue<>(); // Add 2 strings to PriorityQueue. queue.add("cat"); queue.add("bird"); // Loop over and display contents of PriorityQueue. for (String element : queue) { System.out.println(element); } } } Output bird cat
Peek. This method returns the first element in the PriorityQueue (this is the one that is sorted to be first). The contents of the queue are not modified.
Java program that uses peek import java.util.PriorityQueue; public class Program { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>(); // Add data to PriorityQueue. queue.add(1); queue.add(100); queue.add(0); queue.add(1000); // Use peek. // ... The element that is sorted first is returned. // ... The queue is not changed. int peeked = queue.peek(); System.out.println(peeked); } } Output 0
Poll. This method returns the first element in the PriorityQueue and removes it. So the queue's size is reduced by 1. We can call poll() in a loop that checks the size.

Here: We loop infinitely over the PriorityQueue until it has 0 elements. We print all the internally-sorted elements.

Java program that uses poll import java.util.PriorityQueue; public class Program { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>(); // Add data. queue.add(1); queue.add(100); queue.add(0); queue.add(1000); // Poll all elements in the PriorityQueue. while (queue.size() > 0) { // Get polled element and print it. int polled = queue.poll(); System.out.println(polled); } } } Output 0 1 100 1000
Some notes. With PriorityQueue we can access a collection in a sorted order, even without sorting it. We call peek() and poll() to get the first-sorted element.

And: This can simplify code. It can make using elements that must be accessed in a sorted way easier.

With PriorityQueue we have a collection that lets us access data in order—we poll() the first elements, and add them in any order. This helps programs that need priority-based processing.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf