data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

List Interface


Table of Contents


List Interface Characteristics

In Java, the List interface is a part of the Java Collections Framework and represents an ordered collection of elements, allowing duplicates and maintaining the insertion order.

Some key characteristics of the List interface include:

Back to top

Most Used List Implementations

Java provides several implementations of the List interface, each with its own strengths and weaknesses. Here are the most commonly used implementations:

Back to top

ArrayList

It’s generally preferred when frequent element access is required, but not so much for frequent insertions and deletions.

Example:

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

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

        // Adding elements to the ArrayList
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");
        arrayList.add("Grapes");

        // Accessing elements using get()
        System.out.println("Element at index 2: " + arrayList.get(2));

        // Removing elements
        arrayList.remove("Banana");
        System.out.println("After removing 'Banana': " + arrayList);

        // Size of the ArrayList
        System.out.println("Size of the ArrayList: " + arrayList.size());

        // Iteration using for-each loop
        System.out.println("Iterating through the ArrayList:");
        for (String fruit : arrayList) {
            System.out.println(fruit);
        }
    }
}

Back to top

LinkedList

Suitable when you require frequent insertion and deletion, but not ideal for random access.

Example:

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

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

        // Adding elements to the LinkedList
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Orange");
        linkedList.add("Grapes");

        // Accessing elements using get()
        System.out.println("Element at index 2: " + linkedList.get(2));

        // Removing elements
        linkedList.remove("Banana");
        System.out.println("After removing 'Banana': " + linkedList);

        // Size of the LinkedList
        System.out.println("Size of the LinkedList: " + linkedList.size());

        // Iteration using for-each loop
        System.out.println("Iterating through the LinkedList:");
        for (String fruit : linkedList) {
            System.out.println(fruit);
        }
    }
}

Back to top

Choose the appropriate implementation

Always consider the trade-offs and performance characteristics to make an informed decision.

If you need fast random access and infrequent insertion/deletion, use ArrayList.

For frequent insertion and deletion, LinkedList might be a better choice.

Back to top

Comparison Table

Characteristics ArrayList LinkedList
Underlying Data Structure Dynamic Array Doubly Linked List
Access Time Fast O(1) Slow O(n)
Insertion/Deletion Time Slow O(n) Fast O(1)
Random Access Efficient Inefficient [^1]
Memory Overhead Lower Higher
Iteration Performance Faster Slower [^1]
Use Cases Frequent access, infrequent insertion/deletion Frequent insertion/deletion

[^1] Elements are not stored in contiguous memory locations. Instead, each element in a LinkedList is stored in a separate node, and these nodes are connected through pointers (references) to the previous and next nodes, forming a chain-like structure.

Back to top


Ref.

https://www.geeksforgeeks.org/list-interface-java-examples/ https://docs.oracle.com/javase/8/docs/api/java/util/List.html


Get Started | Languages | Java Development | Original Collections API | Updated Collections