Write a Java program to remove an item from the Queue


The Java code represents a program that uses a Queue to store a list of colors and then removes an item from the queue. The Queue is implemented using a LinkedList. Let's go through the code step-by-step:

  • First, a Queue of strings called queue_list is created, and a LinkedList is used as its underlying implementation.
  • Several colors are added to the queue using the add() method.
  • The current state of the queue is printed using System.out.println().
  • The remove() method is called on the queue to remove the first item from the queue. The removed item is then printed using System.out.println().
  • Finally, the updated state of the queue (after removal) is printed again using System.out.println().

Source Code

import java.util.LinkedList;
import java.util.Queue;
public class RemoveItem
{
	public static void main(String[] args)
	{
		Queue < String > queue_list = new LinkedList < > ();
		queue_list.add("Yellow");
		queue_list.add("Green");
		queue_list.add("Pink");
		queue_list.add("Black");
		queue_list.add("Blue");
		queue_list.add("White");
		queue_list.add("Red");
 
		System.out.println("Queue before Removal : " + queue_list);
 
		System.out.println("Removed Item is : " + queue_list.remove());
 
		System.out.println("Queue after Removal : " + queue_list);
	}
}

Output

Queue before Removal : [Yellow, Green, Pink, Black, Blue, White, Red]
Removed Item is : Yellow
Queue after Removal : [Green, Pink, Black, Blue, White, Red]

Example Programs