Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove an element from a Queue in Java
To remove an element from a Queue, use the remove() method.
First, set a Queue and insert some elements −
Queue<String> q = new LinkedList<String>();
q.offer("abc");
q.offer("def");
q.offer("ghi");
q.offer("jkl");
q.offer("mno");
q.offer("pqr");
q.offer("stu");
q.offer("vwx");
Remove the first element −
System.out.println("Queue head = " + q.element());
System.out.println("Removing element from queue = " + q.remove());
The following is an example −
Example
import java.util.LinkedList;
import java.util.Queue;
public class Demo {
public static void main(String[] args) {
Queue<String> q = new LinkedList<String>();
q.offer("abc");
q.offer("def");
q.offer("ghi");
q.offer("jkl");
q.offer("mno");
q.offer("pqr");
q.offer("stu");
q.offer("vwx");
System.out.println("Queue head = " + q.element());
System.out.println("Removing element from queue = " + q.remove());
System.out.println("\nRemaining Queue elements...");
Object ob;
while ((ob = q.poll()) != null) {
System.out.println(ob);
}
}
}
Output
Queue head = abc Removing element from queue = abc Queue head now = abc Remaining Queue elements... def ghi jkl mno pqr stu vwx
Advertisements