Array of Linked List in Java

  1. Demonstrate the Linked List Array Using a Traditional Array in Java
  2. Demonstrate the Linked List Array Using the Constructor in Java

A LinkedList is the sequential and linear data structure that stores elements in a defined order. The data structure is a part of the Collection interface and is present in the java.util package.

The linked list also has elements stored in the node part. Each node has a data part for element storage and the pointer to keep the address of the next node. The elements in the list are not present in contiguous memory locations.

Demonstrate the Linked List Array Using a Traditional Array in Java

Below is the code block that creates an array of linked lists using loops.

import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        LinkedList[] list = new LinkedList[5];
        for (int i = 0; i < 5; i++) {
            if (list[i] == null) {
                list[i] = new LinkedList();
                int temp = i;
                for (int j = 0; j < temp + 1; j++) {
                    list[i].add(j);
                }
            }
            System.out.print(list[i]);
        }
    }
}

In the block above, the new LinkedList[5] statement creates a linked list. The new keyword calls the public constructor of the class linked list. The value 5 demonstrates the size of the array, so we create an array of 5 linked lists.

Over the list variable, the loop runs to instantiate a new linked list over each node. So a for loop gets applied with a condition of integer value less than 5 starts running. Internally, it checks the condition if the value in the node is null; otherwise, a new linked list node instantiates.

Again, a for loop gets used to fill the elements in the list. The add method is used to append the elements at the end of the list. The method is from the LinkedList class and returns the boolean value. It returns true if the add function gets successfully done, else false.

Similarly, the iteration continues, and each node’s value gets filled with the linked list. The same is also printed inside the loop to check the elements present in the nodes.

Output:

[0][0, 1][0, 1, 2][0, 1, 2, 3][0, 1, 2, 3, 4]

Demonstrate the Linked List Array Using the Constructor in Java

Below is the code block that uses Java 8 features and functions to create an array of linked lists.

import java.util.ArrayList;
import java.util.LinkedList;


public class main {

    public static void main(String[] args)
    {
        LinkedList list1 = new LinkedList<String>();
        list1.add("l1_value1");
        list1.add("l1_value2");

        LinkedList list2 = new LinkedList();
        list2.add("l2_value1");
        list2.add("l2_value2");

        LinkedList list3 = new LinkedList();
        list3.add("l3_value1");
        list3.add("l3_value2");

        ArrayList<LinkedList> arrayList = new ArrayList<LinkedList>();
        arrayList.add(list1);
        arrayList.add(list2);
        arrayList.add(list1);
        arrayList.forEach(System.out::println);
        System.out.println("Classname: " + arrayList.get(0).getClass());
    }
}

In the code block above, the main method holds the actual logic for the code execution.

Firstly, a linked list is created using the new keyword. The keyword invokes the public constructor of the LinkedList class. The string values get inserted into the list. Similarly, another two lists are created.

Finally, an array list gets instantiated. The list variables formed are added in the array list instance. The iteration of the list instance happens using the forEach function. Additionally, a print statement with the method reference operator, ::, is added to show the name of instance type present in the array list.

The output of the program above is shown below.

[l1_value1, l1_value2]
[l2_value1, l2_value2]
[l1_value1, l1_value2]
Classname: class java.util.LinkedList

Related Article - Java Linked List

  • Print Linked List in Java
  • Sort Linked List in Java
  • Doubly Linked List in Java
  • Related Article - Java Array

  • Print Linked List in Java
  • Sort Linked List in Java
  • Doubly Linked List in Java