The Java code demonstrates how to use the Queue interface with the LinkedList class to add elements to a queue and retrieve elements 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 null.
import java.util.LinkedList; import java.util.Queue; public class Retrieve_Element { public static void main(String[] args) { Queue <String> queue = new LinkedList <>(); queue.add("Pink"); queue.add("Blue"); queue.add("Yellow"); queue.add("Green"); queue.add("Black"); queue.add("White"); queue.add("Pink"); queue.add("Orange"); queue.add("Red"); System.out.println("Queue Elements : "+queue); System.out.println("Removed Element : " + queue.poll()); System.out.println("Queue Elements : "+queue); } }
Queue Elements : [Pink, Blue, Yellow, Green, Black, White, Pink, Orange, Red] Removed Element : Pink Queue Elements : [Blue, Yellow, Green, Black, White, Pink, Orange, Red]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions