#1
If you're just getting started with Java, one of the most useful tools you'll work with is the List interface. Whether you’re storing numbers, names, or custom objects, Lists make it easy to manage and organize your data.
In this article, we’ll walk you through how to use Lists in Java—what they are, how to create them, add elements, remove items, and loop through them. By the end, you’ll be more confident using Lists in your own Java projects. Let’s get started!

What is a List in Java?

A List is a type of collection in Java that stores an ordered group of elements. Lists can contain duplicate items and you can access elements by their position (called an index), just like an array—but with more flexibility. In Java, List is an interface, which means you can’t create a List directly. Instead, you use one of its implementing classes—like ArrayList, LinkedList, or Vector. For beginners, the most commonly used implementation is ArrayList.

How to Create a List

Here’s how to create a List using ArrayList:
import java.util.ArrayList;
import java.util.List;

public class ListExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
    }
}
In the example above:
  • List<string> means we’re creating a list that will store strings (like “apple” or “banana”).
  • new ArrayList<>() creates a new ArrayList instance.
  • We’re using <> (called the diamond operator) to tell Java the type once, so it doesn’t need to be repeated.
You can also use other types:
List<integer> numbers = new ArrayList<>();
List<double> prices = new ArrayList<>();

Adding Elements to a List

To add elements, use the .add() method:
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
Now, your fruits list contains three items. You can even add duplicates:
fruits.add("Apple"); // It's okay to add "Apple" again

Accessing Elements in a List

To access items in a list, use .get(index), where the index starts at 0.
String firstFruit = fruits.get(0);
System.out.println("The first fruit is: " + firstFruit);
If you try to access an index that doesn’t exist, Java will throw an IndexOutOfBoundsException, so make sure the index is valid!

Looping Through a List

There are many ways to loop through a list. Here are a few popular ones:

1. For Loop (Index-Based)

for (int i = 0; i < fruits.size(); i++) {
    System.out.println(fruits.get(i));
}

2. Enhanced For Loop (For-Each)

for (String fruit : fruits) {
    System.out.println(fruit);
}

3. Using Lambda (Java 8+)

fruits.forEach(fruit -> System.out.println(fruit));
All of these will print each item in the list.

Removing Items from a List

You can remove elements by index or by value.

Remove by index:

fruits.remove(1); // removes the second item (Banana)

Remove by value:

fruits.remove("Apple"); // removes the first occurrence of "Apple"

Clearing the List

Want to empty the whole list?
fruits.clear();
Now the list is empty and fruits.size() will return 0.

Checking List Properties

Here are some helpful methods you can use:
fruits.isEmpty();         // returns true if list is empty
fruits.size();            // returns the number of elements
fruits.contains("Mango"); // returns true if "Mango" is in the list

Bonus: Using Lists with Custom Objects

You’re not limited to Strings and numbers. You can also store custom objects in a list. Here’s an example:
public class Product {
    String name;
    double price;

    Product(String name, double price) {
        this.name = name;
        this.price = price;
    }
}
Then in your main class:
List<product> products = new ArrayList<>();
products.add(new Product("Laptop", 899.99));
products.add(new Product("Headphones", 199.99));
Loop through and print product names:
for (Product p : products) {
    System.out.println(p.name + " - $" + p.price);
}
Link Github here

image quote pre code