You can implement Queue Data Structure (based on FIFO algorithm) in Java using the Queue interface or the LinkedList class.
Let us take a look at queue implementation using both of them:
1. Implementing Queue using LinkedList
package org.code2care.collections;
import java.util.LinkedList;
/**
* Implementing Queue in Java using
* LinkedList
*
* Author: Code2care.org
* Date: 2nd May 2023
* Version: v1.0
*
*/
public class JavaQueueLinkedListExample {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
//Adding elements to queue
queue.add("USA");
queue.add("Japan");
queue.add("China");
queue.add("Canada");
queue.add("India");
//Printing the elements of the Queue
for (String element : queue) {
System.out.println(element);
}
}
}
2. Implementing Queue using Queue Interface
package org.code2care.java.sorting.algo;
import java.util.LinkedList;
import java.util.Queue;
public class QueueExampleUsingQueueInterface {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("China");
queue.add("Japan");
queue.add("USA");
queue.add("Australia");
for (String element : queue) {
System.out.println(element);
}
}
}
Which one to choose?
If you need a simple Queue implementation with basic functionality, make use of the Queue interface. If you need more advanced functions such as adding/removing elements from specific positions make use of the LinkedList implementation.

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!