Example 1 — Add Tasks to the End of a List
Add three tasks one by one, print the list, and check the value that add returns.
import java.util.LinkedList;
public class LinkedListAddDemo {
public static void main(String[] args) {
LinkedList<String> tasks = new LinkedList<>();
boolean first = tasks.add("Write report");
tasks.add("Send email");
tasks.add("Book meeting");
System.out.println("tasks: " + tasks);
System.out.println("add returned: " + first);
System.out.println("size: " + tasks.size());
}
} How It Works
- Start empty.
new LinkedList<>()makes a list with no elements. - Each add goes to the back. "Write report" is added first, so it stays at the front, and "Book meeting" ends up last.
- The return value.
addreportstrue, which is whyfirstprints true. - Three elements. After three calls the list holds three tasks, so
size()is 3.

