Queue vs List Methods on LinkedList
What you'll learn: How the same LinkedList object can be manipulated as both a Queue and a List, and when to use each interface's methods.
The Same Object, Two Perspectives
LinkedList implements both List and Queue, which means you can use either interface's methods on the same object. This is like having a line of people that you can either treat as a strict queue (front-to-back processing) or as a flexible list (access anywhere).
Queue Methods vs List Methods
When you use Queue methods on a LinkedList:
offer(e)adds to the end of the queue (likeadd(e)on List)poll()removes and returns from the front (index 0)peek()looks at the front without removing (likeget(0))
When you use List methods on the same LinkedList:
add(e)appends to the endadd(index, e)inserts anywhereremove(index)removes from any positionget(index)accesses any element
LinkedList<String> data = new LinkedList<>();
// Queue perspective: FIFO operations
data.offer("First"); // add to end
data.offer("Second"); // add to end
data.poll(); // removes "First" from front
// List perspective: random access
data.add(0, "NewFirst"); // insert at front
data.get(1); // access "Second" at index 1
data.remove(1); // remove from middle
When to Use Each
Use Queue methods when you want to enforce FIFO discipline—processing items in order from front to back. Use List methods when you need random access or want to insert/remove from anywhere in the sequence.
Key Takeaway: LinkedList supports both Queue operations (offer/poll for FIFO) and List operations (add/remove by index), giving you flexibility to choose the right interface for your task—but Queue methods communicate intent more clearly when order matters.