Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

List Interface → Overview

List Interface

Overview

List Interface in the Java Collections Framework

The List interface forms a fundamental part of Java's collection framework. It represents ordered collections where elements have a defined position (index). This interface extends the Collection interface and provides additional methods specific to maintaining an ordered sequence of elements. Key characteristics of List: Order: Elements are maintained in the order they were added. Duplicates: Duplicates are allowed. Indexed access: Elements can be accessed by their index (position) using zero-based indexing (first element at index 0). Common Implementations of List: ArrayList: Resizable array-based list. Offers fast random access (getting elements by index) but slower insertions/removals in the middle. LinkedList: Doubly-linked list. Enables efficient insertions/removals at any position but has slower random access compared to ArrayList. Vector: Similar to ArrayList but thread-safe (synchronized), making it less performant for single-threaded access.

Methods of the List Interface:

add(E element): Adds the specified element to the end of the list. add(int index, E element): Inserts the element at the specified index, shifting elements at or after that index to the right. get(int index): Returns the element at the specified index. remove(int index): Removes the element at the specified index and returns it. Elements after the removed element are shifted to the left. indexOf(Object o): Returns the index of the first occurrence of the specified element in the list, or -1 if not found. lastIndexOf(Object o): Returns the index of the last occurrence of the specified element in the list, or -1 if not found. set(int index, E element): Replaces the element at the specified index with the given element. subList(int fromIndex, int toIndex): Returns a sublist view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive).

Choosing the right List implementation:

⯄ If frequent random access is needed, ArrayList is a good choice due to its fast retrieval by index. ⯄ If insertions/removals in the middle are frequent, LinkedList is preferable due to its efficiency in these operations. ⯄ If thread safety is a critical concern, Vector can be used, but be aware of the performance trade-off. Example: Using ArrayList:
List interface basic example using arraylist import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create an ArrayList ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); // Access elements by index String firstFruit = fruits.get(0); // "Apple" // Insert element at specific index fruits.add(1, "Mango"); // Remove element by index fruits.remove(2); // Remove "Orange" // Iterate through the list for (String fruit : fruits) { System.out.println(fruit); } } }

Output

Apple Mango Orange
This example demonstrates basic operations like adding, accessing, removing, and iterating through elements in an ArrayList.

Tutorials