Create Array of Linked Lists in Java

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.

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.

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.

Output:

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *