import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queueq = new LinkedList<>(); for(int i=1;i<=5;i++){ q.add(i); } System.out.println("Elements of queue: " + q); int removed_element = q.poll(); System.out.println("Removed element: " + removed_element); System.out.println("Elements of queue after removing: " + q); } }
import java.util.PriorityQueue; import java.util.Queue; public class QueueExample { public static void main(String[] args) { PriorityQueueIn this example, we have created a PriorityQueue and added three String elements to it. We have then used the poll method to remove the head element and store it in a variable. Finally, we have printed the elements of the queue after the removal. Package library used: java.utilq = new PriorityQueue<>(); q.add("Apple"); q.add("Orange"); q.add("Mango"); System.out.println("Elements of queue: " + q); String removed_element = q.poll(); System.out.println("Removed element: " + removed_element); System.out.println("Elements of queue after removing: " + q); } }