You are currently viewing Java Collections Lists Tutorial

Java Collections Lists Tutorial

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:April 19, 2024

In Java, the java.util.List interface represents an ordered collection of elements. Lists allow duplicates and maintain the insertion order of elements. There are several implementations of the List interface provided by the Java Collections Framework, each with its own characteristics and use cases. In this tutorial, we’ll cover some common implementations of the List interface and how to work with them.

Table of Contents

  1. Introduction to Lists
  2. ArrayList
  3. LinkedList
  4. Vector
  5. Stack
  6. Working with Lists
  1. Conclusion

1. Introduction to Lists

A List in Java is an ordered collection of elements. It allows duplicate elements and maintains the insertion order of elements. The List interface extends the Collection interface and provides additional functionalities for working with ordered collections.

2. ArrayList

ArrayList is one of the most commonly used implementations of the List interface. It internally uses a dynamic array to store elements. Here’s how you can create and use an ArrayList:

Creating an ArrayList

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

public class ArrayListExample {
    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();

        // Adding elements
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");

        // Accessing elements
        System.out.println("First element: " + arrayList.get(0));

        // Iterating over the list
        for (String fruit : arrayList) {
            System.out.println(fruit);
        }

        // Removing elements
        arrayList.remove("Banana");
    }
}

3. LinkedList

LinkedList is another implementation of the List interface. It uses a doubly-linked list internally to store elements. Here’s an example of using LinkedList:

Creating a LinkedList

import java.util.LinkedList;
import java.util.List;

public class LinkedListExample {
    public static void main(String[] args) {
        List<String> linkedList = new LinkedList<>();

        // Adding elements
        linkedList.add("Red");
        linkedList.add("Green");
        linkedList.add("Blue");

        // Accessing elements
        System.out.println("First element: " + linkedList.get(0));

        // Iterating over the list
        for (String color : linkedList) {
            System.out.println(color);
        }

        // Removing elements
        linkedList.remove("Green");
    }
}

4. Vector

Vector is a synchronized implementation of the List interface, meaning it is thread-safe. However, due to its synchronization, it may have lower performance compared to ArrayList. Here’s an example of using Vector:

Creating a Vector

import java.util.Vector;
import java.util.List;

public class VectorExample {
    public static void main(String[] args) {
        List<Integer> vector = new Vector<>();

        // Adding elements
        vector.add(10);
        vector.add(20);
        vector.add(30);

        // Accessing elements
        System.out.println("First element: " + vector.get(0));

        // Iterating over the list
        for (Integer num : vector) {
            System.out.println(num);
        }

        // Removing elements
        vector.remove(1);
    }
}

5. Stack

Stack is a subclass of Vector that represents a last-in, first-out (LIFO) stack of objects. It provides push() and pop() operations to add and remove elements from the top of the stack. Here’s an example of using Stack:

Creating a Stack

import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();

        // Pushing elements
        stack.push("Java");
        stack.push("Python");
        stack.push("C++");

        // Accessing elements
        System.out.println("Top element: " + stack.peek());

        // Popping elements
        String poppedElement = stack.pop();
        System.out.println("Popped element: " + poppedElement);

        // Checking if the stack is empty
        System.out.println("Is stack empty? " + stack.isEmpty());
    }
}

6. Working with Lists

Adding Elements

You can add elements to a list using the add() method:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");

Accessing Elements

You can access elements by their index using the get() method:

String fruit = list.get(0); // Gets the first element

Removing Elements

You can remove elements by their value or index:

list.remove("Banana"); // Removes "Banana"
list.remove(0); // Removes the first element

Iterating Over a List

You can iterate over a list using a for-each loop or an iterator:

for (String item : list) {
    System.out.println(item);
}

// Using an iterator
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    String item = iterator.next();
    System.out.println(item);
}

7. Conclusion

In this tutorial, we covered some common implementations of the List interface in the Java Collections Framework: ArrayList, LinkedList, Vector, and Stack. Each implementation has its own characteristics and use cases. We also looked at how to work with lists, including adding, accessing, removing elements, and iterating over the elements. Understanding these concepts will help you effectively use lists in your Java applications.

Additional Resources:

Leave a Reply