How to Use List Data Structure in Java? (ArrayList Guide)
List interface in Java allows you to store multiple elements sequentially.
Although it works similarly to arrays, it is much more flexible and can grow and shrink dynamically, thanks to its applications such as ArrayList**.
🧠 Technical Summary
Main topic: Java List data structure (specifically ArrayList).
Problem it solves: Index-based collection management whose size changes dynamically.
Steps:
- Create a list
- Adding, changing, deleting elements
- Querying with useful methods (equals, contains, indexOf, toString)
- Learning list operations with practical examples
🔍 List Types and Usage Areas
List is included in the java.util package and has different applications:
| Genre | Feature | Usage Status |
|---|---|---|
| ArrayList | Fast, light, dynamic size | General usage (this is explained in the guide) |
| LinkedList | Fast insertion and deletion | Frequently updated lists |
| Vector | Thread-safe (multi-thread safety) | For synchronized operations |
💡 Tip: In single-threaded projects,
ArrayListis generally the best choice in terms of performance.
⚙️ 1. Creating a List
// String türünde bir ArrayList oluşturur.
List<String> evcilHayvanlar = new ArrayList<>();
Output: pets ==> []
Java lists only store objects. Primitive types (int, boolean, etc.) cannot be added directly — wrapper classes such as Integer, Boolean must be used.
➕ 2. Adding and Updating Elements
Adding Elements
// Listenin sonuna "Köpek" ekler.
evcilHayvanlar.add("Köpek");
To add an element to a specific index:
// "Kedi" elemanını 0. indekse (listenin başına) ekler.
evcilHayvanlar.add(0, "Kedi");
Result: [Cat, Dog]
Element Update
// 0. indeksteki değeri "Kuş" ile değiştirir.
evcilHayvanlar.set(0, "Kuş");
The list is now: [Bird, Dog]
🗑️ 3. Delete Element
// "Köpek" değerini siler.
evcilHayvanlar.remove("Köpek");
// Çıktı: true
To empty the list completely:
evcilHayvanlar.clear();
The list becomes empty: []
🧩 4. Useful List Methods
| Method | Description | Example |
|---|---|---|
add() | Adds element | evcilHayvanlar.add("Kuş") |
set() | Replaces element at specific index | evcilHayvanlar.set(1, "Tavşan") |
remove() | Deletes element | evcilHayvanlar.remove("Kedi") |
clear() | Clears the list | evcilHayvanlar.clear() |
size() | Returns the number of elements | int n = evcilHayvanlar.size() |
isEmpty() | Checks if the list is empty | evcilHayvanlar.isEmpty() |
🔎 5. Query the List
Equality Comparison (equals)
List<String> liste1 = Arrays.asList("Kedi", "Köpek");
List<String> liste2 = Arrays.asList("Kedi", "Köpek");
liste1.equals(liste2); // true
Does It Contain ### Elements? (contains)
evcilHayvanlar.contains("Tavşan"); // true
Finding Index (indexOf)
evcilHayvanlar.indexOf("Kedi"); // 1
Converting List to Text Format (toString)
System.out.println(evcilHayvanlar.toString());
// Çıktı: [Tavşan, Kedi]
🔁 6. Navigating the List with the forEach Method
// Listedeki tüm elemanları sırayla yazdırır.
evcilHayvanlar.forEach(x -> System.out.println(x));
Output: Rabbit Cat
This method is shorter and more readable than classical loops. It also works compatible with the Consumer interface.
❓ Frequently Asked Questions (FAQ)
- Why is List more advantageous than arrays?
Because its size can change dynamically, adding/removing elements is easy.
- Why can't lists hold primitive types?
They only store objects. Integer should be used instead of int, Character should be used instead of char.
- What is the difference between ArrayList and LinkedList?
ArrayList is fast for access while LinkedList is efficient for frequent insertions/deletions.
- Why is Vector not preferred?
It creates performance loss in every operation due to synchronization.
- What to do to check if the list is empty?
isEmpty() method can be used.
🏁 Conclusion
In this guide, you learned how to create a list, add elements, update, delete and query using ArrayList in Java. The List structure is one of the most flexible and powerful tools for data management in modern Java projects.

