List Collections in Java

Master the List interface, ArrayList, LinkedList, Vector, and list operations including adding, removing, updating, and retrieving elements for the OCP 21 exam.

Table of Contents

1. List Interface

The List interface extends Collection and represents an ordered collection (sequence) that allows duplicate elements.

1.1 List Characteristics

  • Ordered: Elements maintain insertion order
  • Indexed: Elements accessed by index (0-based)
  • Duplicates: Allows duplicate elements
  • Null Elements: Allows null elements (implementation-dependent)

1.2 Common List Implementations

  • ArrayList : Resizable array implementation
  • LinkedList : Doubly-linked list implementation
  • Vector : Synchronized resizable array (legacy)

2. ArrayList

ArrayList is a resizable array implementation of the List interface. It's the most commonly used List implementation.

2.1 Creating ArrayList

Example:
import java.util.*;
// Create empty ArrayList List<
String>
list1 = new ArrayList<
>
();
// Create with initial capacity List<
String>
list2 = new ArrayList<
>
(50);
// Create from another collection List<
String>
list3 = new ArrayList<
>
(Arrays.asList("A","B","C"));
// Using List.of() (Java 9+) - immutable List<
String>
list4 = List.of("A","B","C");
// Using Arrays.asList() - fixed-size List<
String>
list5 = Arrays.asList("A","B","C");

2.2 ArrayList Features

  • Dynamic resizing (grows automatically)
  • Fast random access (O(1) for get/set)
  • Fast insertion/removal at end (O(1) amortized)
  • Slow insertion/removal in middle (O(n))
  • Not thread-safe
  • Allows null elements

3. LinkedList

LinkedList is a doubly-linked list implementation. It also implements Deque interface.

3.1 Creating LinkedList

Example:
import java.util.*;
// Create empty LinkedList List<
String>
list1 = new LinkedList<
>
();
// Create from collection List<
String>
list2 = new LinkedList<
>
(Arrays.asList("A","B","C"));
// LinkedList also implements Deque Deque<
String>
deque = new LinkedList<
>
();

3.2 LinkedList Features

  • Fast insertion/removal at any position (O(1) if have reference)
  • Slow random access (O(n))
  • Implements both List and Deque
  • More memory overhead (stores pointers)
  • Not thread-safe
  • Allows null elements

4. Vector

Vector is a legacy synchronized resizable array. It's similar to ArrayList but thread-safe.

4.1 Vector Characteristics

Note: Vector is legacy. Use ArrayList with Collections.synchronizedList() or CopyOnWriteArrayList for thread-safety.
Example:
import java.util.*;
// Create Vector Vector<
String>
vector = new Vector<
>
();
// Vector is synchronized (thread-safe) // But slower than ArrayList due to synchronization
overhead

5. List Operations

5.1 Adding Elements

Example:
import java.util.*;
List<
String>
list = new ArrayList<
>
();
// Add to end list.add("Apple");
// Returns true list.add("Banana");
// Returns true list.add("Cherry");
// Returns true // Add at specific index list.add(1,"Orange");
// Inserts at index 1 // List: [Apple, Orange, Banana, Cherry] // Add all elements from collection list.addAll(Arrays.asList("Grape","Kiwi"));
// Add all at specific index List<
String>
more = Arrays.asList("Mango","Papaya");
list.addAll(2, more);

5.2 Removing Elements

Example:
List<
String>
list = new ArrayList<
>
();
list.addAll(Arrays.asList("Apple","Banana","Cherry","Apple"));
// Remove by object (removes first occurrence) boolean removed = list.remove("Apple");
// true, removes first"Apple" // Remove by index String removedItem = list.remove(0);
// Returns removed element // Remove all occurrences list.removeAll(Arrays.asList("Banana"));
// Removes all"Banana" // Remove if condition list.removeIf(s -> s.startsWith("C"));
// Removes all starting with"C" // Clear all elements list.clear();
// Removes all
elements

5.3 Updating Elements

Example:
List<
String>
list = new ArrayList<
>
();
list.addAll(Arrays.asList("Apple","Banana","Cherry"));
// Set element at index (replaces) String old = list.set(1,"Orange");
// Returns"Banana" // List: [Apple, Orange, Cherry] // Replace all elements list.replaceAll(s -> s.toUpperCase());
// List: [APPLE, ORANGE,
CHERRY]

5.4 Retrieving Elements

Example:
List<
String>
list = new ArrayList<
>
();
list.addAll(Arrays.asList("Apple","Banana","Cherry"));
// Get element by index String first = list.get(0);
//"Apple" String last = list.get(list.size() - 1);
//"Cherry" // Get index of element int index = list.indexOf("Banana");
// 1 int lastIndex = list.lastIndexOf("Apple");
// -1 if not found // Check if contains boolean hasApple = list.contains("Apple");
// true // Get sublist (view, not copy) List<
String>
sublist = list.subList(1, 3);
// [Banana, Cherry] sublist.set(0,"Orange");
// Modifies original list! // List: [Apple, Orange,
Cherry]

6. List Methods

6.1 Common List Methods

Example:
List<
String>
list = new ArrayList<
>
();
// Size and emptiness int size = list.size();
// Number of elements boolean empty = list.isEmpty();
// true if empty // Iteration for (String item : list) {
    System.out.println(item);
} // Using iterator Iterator<
String>
it = list.iterator();
while (it.hasNext()) {
    String item = it.next();
    it.remove();
    // Remove current element } // Using list iterator (bidirectional) ListIterator<
String>
listIt = list.listIterator();
while (listIt.hasNext()) {
    String item = listIt.next();
    int index = listIt.nextIndex();
} // Convert to array String[] array = list.toArray(new String[0]);
// Contains all boolean containsAll =
list.containsAll(Arrays.asList("A","B"));

6.2 List-Specific Methods

Example:
List<
String>
list = new ArrayList<
>
();
list.addAll(Arrays.asList("A","B","C","D","E"));
// Get element by index String element = list.get(2);
//"C" // Set element at index list.set(2,"X");
// Replaces"C" with"X" // Add at index list.add(1,"Y");
// Inserts"Y" at index 1 // Remove by index String removed = list.remove(0);
// Removes and returns"A" // Index of element int index = list.indexOf("B");
// 2 (after insertions) int lastIndex = list.lastIndexOf("B");
// -1 if not found // Sublist (view) List<
String>
sub = list.subList(1, 3);
// [Y,
X]

7. List Implementations Comparison

Feature ArrayList LinkedList Vector
Underlying Structure Dynamic array Doubly-linked list Dynamic array
Random Access O(1) - Fast O(n) - Slow O(1) - Fast
Insert/Remove End O(1) amortized O(1) O(1) amortized
Insert/Remove Middle O(n) O(1) if have reference O(n)
Thread-Safe No No Yes
Memory Overhead Low High (pointers) Low
When to Use Most cases Frequent insertions/removals Legacy code, avoid

8. Exam Key Points

Critical Concepts for OCP 21 Exam:

  • List Interface: Ordered, indexed, allows duplicates
  • ArrayList: Resizable array, fast random access, O(1) get/set
  • LinkedList: Doubly-linked list, fast insertions, implements Deque
  • Vector: Legacy synchronized ArrayList, avoid using
  • add(): Adds element to end, returns boolean
  • add(index, element): Inserts at specific index
  • remove(): Remove by index or object
  • get(index): Retrieve element by index
  • set(index, element): Replace element at index
  • indexOf(): Returns first index of element, -1 if not found
  • lastIndexOf(): Returns last index of element
  • subList(): Returns view (not copy), changes affect original
  • size(): Returns number of elements
  • isEmpty(): Returns true if empty
  • contains(): Checks if element exists
  • removeIf(): Removes elements matching predicate
  • replaceAll(): Replaces all elements using function
  • ListIterator: Bidirectional iterator for lists
  • toArray(): Converts list to array
  • Null Elements: Lists allow null elements
  • ConcurrentModificationException: Thrown when modifying during iteration

Post a Comment

0 Comments