Support Online
Skip to main content

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:

  1. Create a list
  2. Adding, changing, deleting elements
  3. Querying with useful methods (equals, contains, indexOf, toString)
  4. Learning list operations with practical examples

🔍 List Types and Usage Areas

List is included in the java.util package and has different applications:

GenreFeatureUsage Status
ArrayListFast, light, dynamic sizeGeneral usage (this is explained in the guide)
LinkedListFast insertion and deletionFrequently updated lists
VectorThread-safe (multi-thread safety)For synchronized operations

💡 Tip: In single-threaded projects, ArrayList is 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

MethodDescriptionExample
add()Adds elementevcilHayvanlar.add("Kuş")
set()Replaces element at specific indexevcilHayvanlar.set(1, "Tavşan")
remove()Deletes elementevcilHayvanlar.remove("Kedi")
clear()Clears the listevcilHayvanlar.clear()
size()Returns the number of elementsint n = evcilHayvanlar.size()
isEmpty()Checks if the list is emptyevcilHayvanlar.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)

  1. Why is List more advantageous than arrays?

Because its size can change dynamically, adding/removing elements is easy.

  1. 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.

  1. What is the difference between ArrayList and LinkedList?

ArrayList is fast for access while LinkedList is efficient for frequent insertions/deletions.

  1. Why is Vector not preferred?

It creates performance loss in every operation due to synchronization.

  1. 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.