Hackforge Academy

Category: java

List in Java

Published on 23 Mar 2026

Explanation


Declaration of List We use interface reference (List) and object of class (ArrayList) β€” Good practice.

Code:

import java.util.List;
import java.util.ArrayList;

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

Explanation

Common List Implementations

Code:

#white-ArrayList	
Fast read, dynamic array

#white-LinkedList	
Fast insert/delete

#white-Vector
Thread-safe (old)

#white-Stack
LIFO structure

Explanation

Using ArrayList

Code:

import java.util.*;

public class ListExample {
    public static void main(String[] args) {

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

   names.add("Java");          
   names.add("Python");
   names.add("Java");   
   System.out.println(names);
    }
}

Explanation


Accessing Elements (Index) names.get(0) Updating Elements names.set(1, "Spring");
Removing Elements names.remove(0); names.remove("Java") Enhance Looping: for (String name : names) { System.out.println(name); }

Code:

Using forEach (Java 8)
names.forEach(n -> 
System.out.println(n)
);

Explanation

Using LinkedList

Code:

public class LinkedListExample {

    public static void main(String[] 
args) {

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

    }
}

Explanation

Sorting List

Code:

List<Integer> nums = new ArrayList<>();
nums.add(5);
nums.add(1);
nums.add(3);

Collections.sort(nums);

Explanation

List with Custom Objects

Code:

List<Student> students = new ArrayList<>();

  students.add(new Student(1, "hackforge"));
  students.add(new Student(2, "java"));

        System.out.println(students);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community