Java Array of Linked Lists Example
Java offers a variety of data structures to meet different use cases. One powerful hybrid data structure is an “array of linked lists.” This combination leverages the indexed structure of arrays with the dynamic nature of linked lists. Let us delve into understanding how a Java array containing linked lists can be created and utilized effectively in real-world applications.
1. What Is an Array of Linked Lists?
An array of linked lists is essentially a hybrid data structure that combines the features of arrays and linked lists. In this structure, each element of the array acts as a reference (or pointer) to a separate LinkedList object. This means that instead of holding a single value, each array element can hold a dynamic collection of elements via the linked list. This is particularly useful when you want fixed-size indexing (like arrays offer) but need dynamic storage at each index (which linked lists provide). In Java, an array of linked lists can be declared and initialized using the following syntax:
LinkedList<Type>[] array = new LinkedList[size];
for (int i = 0; i < size; i++) {
array[i] = new LinkedList<>();
}
1.1 Where Can We Use an Array of Linked Lists?
- Adjacency Lists in Graph Representation: In graph data structures, especially for sparse graphs, each node (represented by an array index) maintains a list of its adjacent nodes. This approach significantly reduces memory consumption compared to a full adjacency matrix.
- Hash Table Chaining for Collision Resolution: When two keys map to the same index in a hash table, a linked list at that index can store all colliding entries. This technique is known as separate chaining and helps handle collisions gracefully.
- Grouping Elements by Some Index (like Student Batches): If you have students grouped by year, department, or performance category, you can use the array index as the group identifier and store student data in the linked list at that index.
- Binning Data into Dynamic Buckets: When categorizing data into dynamic ranges (like histograms or frequency bins), you can use the array as a fixed range of bins, and use linked lists at each index to store the elements falling into that bin.
2. Creating an Array of Linked Lists in Multiple Ways
Below is a single Java class that demonstrates four different ways to create and initialize an array or list of LinkedLists:
- using a traditional raw array with a manual initialization loop
- using a
ListofLinkedListobjects viaArrayList - using Java 8
Streamsfor a functional-style initialization, and - using
Arrays.setAll()for a concise and declarative setup.
// LinkedListArrayExamples.java
import java.util.*;
import java.util.stream.*;
public class LinkedListArrayExamples {
public static void main(String[] args) {
int size = 4;
// Method 1: Traditional Raw Array with Initialization Loop
LinkedList<String>[] array1 = new LinkedList[size];
for (int i = 0; i < size; i++) {
array1[i] = new LinkedList<>();
}
array1[0].add("Apple");
// Method 2: List of LinkedLists
List<LinkedList<String>> listOfLists = new ArrayList<>();
for (int i = 0; i < size; i++) {
listOfLists.add(new LinkedList<>());
}
listOfLists.get(1).add("Banana");
// Method 3: Java 8 Streams
LinkedList<String>[] array2 = IntStream.range(0, size)
.mapToObj(i -> new LinkedList<String>())
.toArray(LinkedList[]::new);
array2[2].add("Cherry");
// Method 4: Arrays.setAll (Java 8+)
LinkedList<String>[] array3 = new LinkedList[size];
Arrays.setAll(array3, i -> new LinkedList<>());
array3[3].add("Date");
// Display all the initialized collections
System.out.println("Method 1 (Raw Array):");
for (int i = 0; i < size; i++) {
System.out.println("Index " + i + ": " + array1[i]);
}
System.out.println("\nMethod 2 (List of LinkedLists):");
for (int i = 0; i < size; i++) {
System.out.println("Index " + i + ": " + listOfLists.get(i));
}
System.out.println("\nMethod 3 (Streams):");
for (int i = 0; i < size; i++) {
System.out.println("Index " + i + ": " + array2[i]);
}
System.out.println("\nMethod 4 (Arrays.setAll):");
for (int i = 0; i < size; i++) {
System.out.println("Index " + i + ": " + array3[i]);
}
}
}
2.1 Code Explanation and Output
The Java program demonstrates four different ways to create and initialize an array of linked lists (or a list of linked lists) using various Java techniques. In Method 1, a traditional raw array of type LinkedList<String> is created and initialized using a loop, where each index is manually assigned a new LinkedList object, and the string “Apple” is added to the first list. Method 2 uses a List of LinkedList objects instead of an array, offering a more flexible alternative through the ArrayList class, and adds “Banana” to the second list. Method 3 demonstrates a concise and functional approach using Java 8’s IntStream to generate and initialize an array of linked lists, with “Cherry” added to the third index. Method 4 uses the Arrays.setAll() method (introduced in Java 8) to initialize a new array of linked lists more declaratively, adding “Date” to the fourth index. Finally, the program prints out the contents of each structure, showing how each index in the array or list contains its own dynamically created and populated linked list.
Method 1 (Raw Array): Index 0: [Apple] Index 1: [] Index 2: [] Index 3: [] Method 2 (List of LinkedLists): Index 0: [] Index 1: [Banana] Index 2: [] Index 3: [] Method 3 (Streams): Index 0: [] Index 1: [] Index 2: [Cherry] Index 3: [] Method 4 (Arrays.setAll): Index 0: [] Index 1: [] Index 2: [] Index 3: [Date]
2.2 Performance Considerations
The table below summarizes the performance and design trade-offs between different ways to create an array or list of linked lists in Java:
| Method | Access Time | Initialization Overhead | Flexibility | Memory Usage | Java Version Support |
|---|---|---|---|---|---|
| Raw Array with Loop | O(1) array access, O(n) list traversal | Manual loop required | Low (fixed size) | Low to Moderate | Java 1.5+ |
| List of LinkedLists (ArrayList) | O(1) list access, O(n) list traversal | Manual loop or Collections.nCopies() | High (dynamic size) | Moderate | Java 1.5+ |
| Java 8 Streams | O(1) array access, O(n) list traversal | Concise, functional style | Medium | Moderate | Java 8+ |
| Arrays.setAll() | O(1) array access, O(n) list traversal | Concise, declarative | Medium | Moderate | Java 8+ |
Note: “n” refers to the number of elements in a specific linked list. Access time refers to retrieving an element from the array or list structure, not from inside the linked list.
3. Conclusion
Creating an array of linked lists in Java provides a flexible way to handle multi-dimensional data or clustered elements. You can initialize them using a raw loop, Java 8 Streams, Arrays.setAll(), or even use Lists for dynamic flexibility. Each approach has its benefits depending on the context and Java version in use.

