Mastering lists in Java is one of the most essential and frequently used skills in any Java tutorial for beginners — lists let you store and manage dynamic collections of objects (strings, numbers, custom classes) with easy resizing, insertion, removal, searching, and sorting. Unlike fixed-size arrays, lists in Java grow or shrink automatically — making them perfect for real-world tasks like user lists, shopping carts, task managers, game inventories, database results, and more.

In this complete lists in Java guide, you’ll focus on the most popular implementation: ArrayList (from java.util). You’ll learn how to:

  • Create and initialize lists
  • Add, remove, modify, and access elements
  • Loop through lists (classic & modern ways)
  • Use powerful List methods (sort, search, contains, indexOf, etc.)
  • Understand why List > arrays in most cases
  • Avoid common beginner mistakes

All examples are tested in jshell (Java’s interactive REPL) — copy-paste to see instant results on Java 21 (LTS in 2025–2026).

Prerequisites

  • Java 11+ installed (preferably 17 or 21 LTS)
  • Terminal + jshell (type jshell to start)
  • Basic knowledge of variables, data types, loops & conditionals

1. Creating Lists in Java – ArrayList Basics

Import first:

				
					import java.util.ArrayList;
import java.util.List;
				
			

Way 1 – Empty list

				
					List<String> pets = new ArrayList<>();
				
			

Way 2 – With initial values

				
					List<String> fruits = new ArrayList<>(List.of("Apple", "Banana", "Cherry"));
				
			

Way 3 – From another collection

				
					Way 3 – From another collection
				
			

Generic type <String> ensures type safety — only strings allowed.

2. Adding & Removing Elements – Core of Lists in Java

				
					List<String> pets = new ArrayList<>();

// Add at end
pets.add("Dog");
pets.add("Cat");

// Add at specific index
pets.add(0, "Fish");           // Fish now first

// Add multiple
pets.addAll(List.of("Bird", "Hamster"));

// Remove by value
pets.remove("Cat");

// Remove by index
pets.remove(1);                 // removes "Dog" if it was at index 1

// Clear all
// pets.clear();
				
			

Check size & emptiness:

				
					System.out.println(pets.size());      // 4
System.out.println(pets.isEmpty());   // false
				
			

3. Accessing & Modifying Elements

				
					String first = pets.get(0);           // Fish
pets.set(0, "Goldfish");              // replace Fish → Goldfish

System.out.println(pets.get(2));      // Bird
				
			

Check existence & position:

				
					System.out.println(pets.contains("Hamster"));   // true
System.out.println(pets.indexOf("Bird"));       // 2
				
			

4. Looping Through Lists in Java

Classic for (with index):

				
					for (int i = 0; i < pets.size(); i++) {
    System.out.println("Pet " + (i+1) + ": " + pets.get(i));
}
				
			

foreach (enhanced for – recommended):

				
					for (String pet : pets) {
    System.out.println("Pet: " + pet);
}
				
			

Modern forEach method (Java 8+):

				
					pets.forEach(pet -> System.out.println("Pet: " + pet));
				
			

5. Powerful List Methods – Must-Know for Lists in Java

				
					// Sort
pets.sort(null);                        // natural order (alphabetical)
pets.sort(String.CASE_INSENSITIVE_ORDER);   // case-insensitive

// Reverse
Collections.reverse(pets);

// Shuffle
Collections.shuffle(pets);

// Fill
pets.replaceAll(String::toUpperCase);   // all uppercase

// Sub-list
List<String> somePets = pets.subList(1, 3);   // from index 1 to 2
				
			

6. Best Practices & Modern Tips (2025–2026)

  • Always use interface List<String> on left, concrete ArrayList<> on right
  • Prefer List.of(…) (Java 9+) for immutable lists
  • Use var (Java 10+) for cleaner declarations:
				
					var animals = new ArrayList<String>();
				
			
  • Avoid raw types (List pets = new ArrayList();) — lose type safety
  • Use ArrayList for most cases — fast random access
  • Switch to LinkedList only when you need frequent inserts/removals in middle
  • Check size() before get(index) to avoid IndexOutOfBoundsException

Lists in Java – FAQ (2025–2026)

  1. How do I create lists in Java?
    List<String> list = new ArrayList<>(); or List.of(“A”, “B”) (immutable)
  2. How do I add/remove elements in lists in Java?
    add(“item”), add(index, “item”), remove(“item”), remove(index)
  3. How do I loop through lists in Java?
    Best: for (String item : list) { … } (foreach)
  4. How do I sort lists in Java?
    list.sort(null); or Collections.sort(list);
  5. ArrayList vs LinkedList in Java?
    ArrayList: fast random access; LinkedList: fast inserts/removals in middle

Summary

You now fully understand lists in Java: ArrayList creation, add/remove/modify, looping, powerful methods (sort, contains, indexOf, etc.), and best practices.

Mastering lists in Java unlocks dynamic data handling — essential for almost every real Java program (user lists, inventories, results, queues, etc.).