/** * @author lautturi.com * Java example: java PriorityQueue/ Priority Queue example in java */ import java.util.*; public class Lautturi { public static void main(String[] args) { PriorityQueue<String> pqueue = new PriorityQueue<String>(); pqueue.add("java"); pqueue.add("lautturi"); pqueue.add("python"); pqueue.add("lautTur1"); // print the Queue System.out.println(pqueue); // insert new element to the queue pqueue.offer("js"); System.out.println(pqueue); // add new value to the Queue pqueue.add("php"); System.out.println(pqueue); // remove and return front value from Queue: String next = pqueue.remove(); System.out.println("next:" + next); // return front value without removing from Queue: String peek = pqueue.peek(); System.out.println("peek:" + peek); System.out.println("The queue:"); // print the Queue System.out.println(pqueue); } }
output:
[java, lautTur1, python, lautturi] [java, js, python, lautturi, lautTur1] [java, js, php, lautturi, lautTur1, python] next:java peek:js The queue: [js, lautTur1, php, lautturi, python]