Java List
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java List

Harine

Java List Interface

The List interface is an essential component of the Java Collections Framework and represents an ordered collection of elements. It allows developers to store, manage, and manipulate a sequence of objects while maintaining the order in which they were inserted.

One of the main advantages of a List is that elements can be accessed using their index, similar to arrays. However, Lists provide far more flexibility because they can dynamically grow or shrink as elements are added or removed.

Because List is an interface, it cannot be instantiated directly. Instead, you must create objects using classes that implement the List interface.

Some commonly used implementations include:

  • ArrayList – A dynamic array that provides fast access to elements using indexes.
  • LinkedList – A doubly linked structure that allows efficient insertion and deletion of elements.

Tip:

Use a List when the order of elements matters, duplicates are allowed, and you need index-based access.

Key Characteristics of the List Interface

A List collection provides several useful features that make it suitable for many real-world applications.

Ordered Collection

Elements are stored in the same order in which they are inserted.

Duplicate Elements Allowed

A List can contain the same value multiple times.

Index-Based Access

Each element has a specific position (index) starting from 0.

Dynamic Size

Unlike arrays, Lists automatically adjust their size when elements are added or removed.

Example Program: Managing a Product List

The following example demonstrates how to use the List interface with an ArrayList implementation to manage product names.

import java.util.ArrayList;

import java.util.List;

public class ProductListExample {

   public static void main(String[] args) {

     // Creating a List using ArrayList implementation

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

     // Adding products

     products.add("Laptop");

     products.add("Keyboard");

     products.add("Mouse");

     products.add("Monitor");

     // Accessing an element by index

     System.out.println("First Product: " + products.get(0));

     // Updating an element

     products.set(2, "Wireless Mouse");

     // Removing an element

     products.remove("Keyboard");

     // Displaying all products

      System.out.println("\nAvailable Products:");

      for(String item : products) {

          System.out.println(item);

      }

      // Displaying total count

      System.out.println("\nTotal Products: " + products.size());

      }

    }

This program demonstrates how a List can be used to store, modify, and retrieve elements dynamically.

Java List vs Array

Although Lists and arrays both store collections of data, they differ significantly in flexibility and functionality

Arrays are useful when the size of data is known in advance, while Lists are better suited for situations where the number of elements may change dynamically.

When to Use a List

A List is the ideal choice when:

  • The order of elements must be preserved
  • Duplicate values are allowed
  • You need quick access using indexes
  • The number of elements may change during program execution

Common real-world examples include:

  • Managing a shopping cart
  • Storing user comments
  • Tracking tasks in a to-do list
  • Maintaining ordered records

Our website uses cookies to enhance your experience. Learn More
Accept !