The Java code demonstrates how to use the Queue interface with the LinkedList class to add elements to a queue and remove elements from the queue using the poll() method. The poll() method removes and retrieves the head of the queue (the first element) if the queue is not empty. If the queue is empty, it returns
import java.util.LinkedList; import java.util.Queue; public class RemoveElement { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.offer("Blue"); queue.offer("White"); queue.offer("Black"); queue.offer("Orange"); queue.offer("Pink"); queue.offer("Yellow"); System.out.println("Queue before removing Elements : " + queue); // Remove elements from the queue using poll() String removedElement1 = queue.poll(); String removedElement2 = queue.poll(); System.out.println("Removed Elements : " + removedElement1 + ", " + removedElement2); System.out.println("Queue after removing Elements : " + queue); } }
Queue before removing Elements : [Blue, White, Black, Orange, Pink, Yellow] Removed Elements : Blue, White Queue after removing Elements : [Black, Orange, Pink, Yellow]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions