Ugenz Academy/Tutorial

List

3 min read596 words

Reading progress

Keep learning

List in Java

Introduction

A List in Java is an ordered collection that allows us to store multiple elements. A List can contain duplicate values and maintains the insertion order.

The List interface is part of the Java Collection Framework and is available in the java.util package.

import java.util.List;

Features of List

  • Maintains insertion order.
  • Allows duplicate elements.
  • Allows null values.
  • Elements can be accessed using an index.
  • Supports dynamic size.
  • Provides methods to add, remove, and update elements.

List Syntax

List<Type> listName = new ArrayList<>();

Example:

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

Example of List

import java.util.*; public class ListExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); names.add("Anu"); System.out.println(names); } }

Output

[Anu, Ram, Sita, Anu]

Here, "Anu" appears twice because a List allows duplicate values.

Common List Methods

MethodDescription
add()Adds an element
get()Returns an element
set()Updates an element
remove()Removes an element
size()Returns number of elements
contains()Checks whether an element exists
clear()Removes all elements
isEmpty()Checks whether the List is empty

Example

List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Banana"); System.out.println(fruits.get(1)); fruits.set(1, "Orange"); fruits.remove("Banana"); System.out.println(fruits);

Output

Mango [Apple, Orange]

Accessing List Elements

List elements can be accessed using their index.

List<String> cities = new ArrayList<>(); cities.add("Janakpur"); cities.add("Kathmandu"); cities.add("Pokhara"); System.out.println(cities.get(0)); System.out.println(cities.get(1));

Output

Janakpur Kathmandu

Note: List indexing starts from 0.

Types of List

Common implementations of the List interface are:

1. ArrayList

ArrayList is commonly used when frequent access to elements is required.

List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); System.out.println(names);

2. LinkedList

LinkedList is useful when elements are frequently added or removed.

List<Integer> numbers = new LinkedList<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers);

3. Vector

Vector is a synchronized List implementation.

List<Integer> numbers = new Vector<>(); numbers.add(10); numbers.add(20); System.out.println(numbers);

List vs Array

ListArray
Dynamic sizeFixed size
Part of Collection FrameworkBasic Java data structure
Provides many built-in methodsFewer built-in methods
Allows easy insertion/removalInsertion/removal is less flexible
Can store objects using genericsCan store primitive types directly

Advantages of List

  1. Maintains insertion order.
  2. Allows duplicate elements.
  3. Provides dynamic size.
  4. Provides many useful methods.
  5. Elements can be accessed using indexes.
  6. Easy to add, remove, and update elements.

Real-Life Example

A List can be used to store:

Student Names -------------Anu Ram Sita Hari Anu

The duplicate name "Anu" is allowed because List supports duplicate elements.

Key Points

  • List is an interface in Java.
  • It belongs to the java.util package.
  • It maintains insertion order.
  • It allows duplicate values.
  • ArrayList, LinkedList, and Vector are common implementations.
  • List indexing starts from 0.
  • List provides methods such as add(), get(), set(), and remove().

Conclusion

A List is one of the most commonly used collections in Java. It is useful when we need an ordered collection that can grow or shrink dynamically and can contain duplicate elements. ArrayList is generally a good starting choice for beginners.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy