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.

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Java 8: Get First and Last Date of the Week for Given Date - Java
- [Solved] SharePoint System.IO.FileNotFoundException was unhandled - SharePoint
- Java: List of Data Structures available - Java
- Create a Gradle Java Project in VS Code - Gradle
- String Boot + Redis - SET and GET String Commands Examples - Java
- How to Change Text Size for Android ActionBar - Android
- How to Kill a port using bash terminal command? - Bash
- bash: netstat: command not found - Bash