Example 1 — Append Orders to the Back of a Queue
Add customer orders to the tail so that they stay in arrival order.
import java.util.LinkedList;
public class LinkedListAddLastDemo {
public static void main(String[] args) {
LinkedList<String> orders = new LinkedList<>();
orders.addLast("Order 101");
orders.addLast("Order 102");
orders.addLast("Order 103");
System.out.println("orders: " + orders);
System.out.println("last: " + orders.getLast());
System.out.println("first: " + orders.getFirst());
}
} How It Works
- Each call goes to the tail. Order 101 arrives first, so it stays at the front while later orders line up behind it.
- Arrival order is kept. The printed list reads 101, 102, 103, exactly as the orders came in.
- The newest is last.
getLast()returns Order 103, the one added most recently. - The oldest is first.
getFirst()returns Order 101, the next one to be handled in a first-in, first-out queue.

