LinkedList Implements Queue: Dual Interfaces
What you'll learn: How LinkedList serves as both a List and a Queue implementation simultaneously.
The Dual Nature of LinkedList
LinkedList is unique in the Java Collections Framework because it implements both the List interface and the Queue interface. This means a single LinkedList object can behave as a traditional list (with indexed access, insertion anywhere) and as a FIFO queue (with enqueue/dequeue operations).
Why This Works
Think of a LinkedList like a flexible rope of connected nodes. When you use it as a List, you can grab any position along the rope. When you use it as a Queue, you strictly add to one end (the tail) and remove from the other (the head). The underlying structure is the same—only the operations you choose to perform change its behavior.
Practical Implications
When you declare a LinkedList, you can reference it through either interface:
// Same object, different interface views
Queue<String> taskQueue = new LinkedList<>();
taskQueue.offer("First task"); // Queue method
taskQueue.poll(); // Queue method
List<String> taskList = new LinkedList<>();
taskList.add(0, "First task"); // List method
taskList.get(0); // List method
Both references point to a LinkedList implementation, but the interface you choose determines which methods are available. This is the power of polymorphism—the object's capabilities remain the same, but you can restrict which behaviors are exposed.
When to Use Each Interface
Choose Queue<E> as your reference type when you want FIFO semantics and only need queue operations. Choose List<E> when you need positional access. The LinkedList underneath handles both efficiently.
Key Takeaway: LinkedList implements both List and Queue interfaces, letting you use the same data structure as either an ordered list with random access or a FIFO queue, depending on which interface you reference it through.