Table of Contents
Introduction
In this article, we will look at how to Create an Array of Linked Lists in Java
. We will look at ways to create an Array of Linked Lists or a List of Linked Lists in detail with working examples. Let us first have a quick peek into the concept of Linked Lists in Java.
Linked List in Java
A Linked List is a linear, dynamic Data Structure that stores homogenous data(data of the same type) following a sequence in non-contiguous memory locations. Each value in the List is represented as a Node that holds the reference to its next consecutive Node in a different memory location.
Example of Linked List:
In Java, the implementation of Linked List is defined in the LinkedList class in java.util package as part of the Collection framework introduced in Java 8.
Application of Array of Linked Lists
There is a crucial need to create an Array of Linked Lists, they have wide use in representing the relationship between the nodes and edges of Graph Data Structure.
Although an Adjacency List using ArrayList is preferred, we can represent the Adjacency matrix representation of a Graph using this Array of Linked Lists. Each index of the array gives us information about the nodes and the Linked List at each index row gives information about the connected edges to the respective node.
Example of Adjacency Matrix or List:
Here, each row starting index indicates the array index which holds a Linked List within itself. The Linked List shows the corresponding edges connected to each of the starting indexes in a connected and directed graph.
Create Array of Linked Lists in Java
Without further ado, we outline different ways to create an Array of Linked Lists in Java.
Using Object[] array of Linked Lists in Java
We can create an Array of Linked Lists by using the Object[] type array.
Important points to note:
- We will create LinkedList objects and initialize each list with dummy values using the add() method.
- We will then create an Object class-type array and initialize the array with the lists.
- As the Object class is the direct parent to all Java classes, it will readily accept the Linked Lists as input.
- Then, we print each List in the Object[] array using the toString() method.
Note: The advantage of using Object[] arr is that it allows us to store Linked Lists having elements of any data type as the Object class is a parent to all classes.
Let us look at the implementation code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.LinkedList; public class ObjectArrayOfLinkedLists { public static void main(String[] args) { // We create a Linked List of String type. LinkedList<String> list1 = new LinkedList<String>(); list1.add("Welcome"); list1.add("to"); list1.add("Java2Blog"); LinkedList<String> list2 = new LinkedList<String>(); list2.add("Enjoy"); list2.add("Learning"); // We create a Linked List of Integer type. LinkedList<Integer> list3 = new LinkedList<Integer>(); list3.add(1); list3.add(2); list3.add(3); // We can add all types of list into Object array. Object[] arr = {list1, list2, list3}; System.out.println("The array of Linked Lists using Object[] array:"); System.out.println(); for(Object list : arr) { System.out.println(list.toString()); } } } |
Output:
Using the Linked List array in Java
We can create an Array of Linked Lists by declaring an Array of LinkedList class type. It will follow the same declaration as an array using the new keyword with a fixed size.
Important points to note:
- Each index of an array holds a Linked List, it is to be ensured that while accessing the array we need to initialize each Linked List object.
- We cannot declare an Array of Linked Lists belonging to a specific type so we declare the LinkedList array of raw or generic type.
- Hence, We initialize each LinkedList of Integer type while accessing the array, like this: arr[i] = new LinkedList<Integer>();
- We fill each list with dummy values and print them accordingly.
Let us look at the code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedList; public class LinkedListArraysExample { public static void main(String[] args) { // We create a Linked List of Generic type. LinkedList linkedList[] = new LinkedList[5]; System.out.println("The Array of Linked List is: \n"); for (int i = 0; i < 5; i++) { // Initialize each Linked List of Integer type within array linkedList[i] = new LinkedList<Integer>(); for (int j = 1; j <= 5; j++) { linkedList[i].add(j); } System.out.print(linkedList[i]); System.out.println(); } } } |
Output:
Using the ArrayList of Linked Lists in Java
Instead of creating an Array of Linked Lists, we can also resolve to create an ArrayList of LinkedList Objects. We will create individual Linked Lists and fill them with sample values. Then, add the Linked Lists to an ArrayList of type LinkedList.
Let us have a quick look at the implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; import java.util.LinkedList; public class ArrayListOfLinkedListExample { public static void main(String[] args) { // We create a Linked List of String type. LinkedList<String> l1 = new LinkedList<String>(); l1.add("Hello!"); l1.add("Welcome to Java2Blog"); LinkedList<String> l2 = new LinkedList<String>(); l2.add("Have Fun"); l2.add("Learning"); ArrayList<LinkedList> arrayList = new ArrayList<LinkedList>(); // we add the Linked Lists arrayList.add(l1); arrayList.add(l2); System.out.println("The ArrayList of Linked List is: \n"); // Use the for each method to print each Linked list arrayList.forEach(System.out::println); } } |
Output:
Further reading:
Using the Apache Commons Collections Package
The Apache Commons API provides powerful and reusable Java components and dependencies that we can embed into our applications and programs. Here, to create an Array of Linked Lists in we can use the inbuilt functionality of this package.
We will use the collections4
package of the Apache Commons Library by importing with the following dependency.
1 2 3 4 |
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> <dependency org="org.apache.commons" name="commons-collections4" rev="4.4"/> |
Important points to note:
- We will create a Linked List reference of the AbstractLinkedList type and then instantiate it using the CursorableLinkedList class.
- We then insert these Linked lists into an Object[] type array.
- Then, we print these Linked Lists using the toString() method.
Let us look at the implementation code for this approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import org.apache.commons.collections4.list.AbstractLinkedList; import org.apache.commons.collections4.list.CursorableLinkedList; public class ApacheCommonExample { public static void main(String[] args) { // We create a AbstractLinkedList reference l1 // Instantiate l1 using the CursorableLinkedList Class. AbstractLinkedList<Integer> l1 = new CursorableLinkedList<Integer>(); l1.add(1); l1.add(2); l1.add(3); // We create another Linked List of same type AbstractLinkedList<Integer> l2 = new CursorableLinkedList<Integer>(); l2.add(4); l2.add(5); l2.add(6); // We create a Object Array with the Linked Lists. Object arr[] = {l1,l2}; System.out.println("The Array of Linked Lists using Apache Commons Collections Package: "); // Iterate through each list and print its equivalent toString() for(Object list : arr) { System.out.println(list.toString()); } } } |
Output:
That’s all for the article we had a look at 4 different ways to Create an Array of Linked Lists in Java
with working examples in detail. You can try out the examples in your Local Compiler/IDE for a clear understanding.
Feel free to reach out to us for any suggestions/doubts.