Java Program to Add elements to a LinkedList


LinkedList is a generic class of Java Collection Framework that implements three interfaces namely List, Deque, and Queue. It provides the features of a LinkedList data structure which is a linear data structure where each element are linked with each other. There are several operations we can perform on LinkedList including appending, removing and traversing of elements. To add elements to a LinkedList Collection we can use various built-in methods such as add(), addFirst() and addLast(). We are going to explore how we can use these methods to add elements to a LinkedList.

Adding elements to a LinkedList in Java

In Java, LinkedList class provides the following built-in methods to add elements −

  • add() − It is used to insert an element at the end of the collection. We use it more frequently than other methods.

  • addFirst() − This method is used to insert an element at the first index of LinkedList.

  • addLast() − It inserts an element at the last index of the specified LinkedList.

  • addAll() − When we require to add all the elements of one collection to another LinkedList, then we use addAll() method.

In our Java programs, while inserting the elements to the LinkedList we simply need to pass the required elements as an argument.

Before jumping to the Java program to add elements to a LinkedList, let's discuss the syntax to create an instance of LinkedList class.

Syntax

LinkedList<Type> nameOfinstance = new LinkedList<>();

Here, 'Type' could be any wrapper class.

Example 1

The following example illustrates how we can insert elements to the LinkedList using add() method.

import java.util.LinkedList;
public class Demo1 {
   public static void main(String[] args) {
      // creating a linkedlist 
      LinkedList<String> input_list = new LinkedList<>();
      // adding elements to the list
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("Scala");
      input_list.add("Shell");
      // printing the result
      System.out.println("The elements added to the list are: " + input_list);
   }
}

Output

The elements added to the list are: [Java, Python, Scala, Shell]

Example 2

It is also possible to insert elements at the required positions of LinkedList using the add() method. It can also accept an index number as an optional argument.

import java.util.LinkedList;
public class Demo2 {
   public static void main(String[] args) {
      // creating a linkedlist 
      LinkedList<String> input_list = new LinkedList<>();
      // adding initial elements to the list
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("JavaScript");
      // printing the result
      System.out.println("The list is defined as: " + input_list);
      // adding a new element to the existing list at index 1
      input_list.add(1, "Scala");
      // printing the new result
      System.out.println("The list after adding element at position 1: ");
      int index = 0;
      for(String print : input_list) {
         System.out.println("Index: " + index + ", Element: " + print);
         index++;
      }
   }
}

Output

The list is defined as: [Java, Python, JavaScript]
The list after adding element at position 1: 
Index: 0, Element: Java
Index: 1, Element: Scala
Index: 2, Element: Python
Index: 3, Element: JavaScript

Example 3

In the following example, we will add elements to the LinkedList using listIterator() method.

import java.util.LinkedList;
import java.util.ListIterator;
public class Demo3 {
   public static void main(String[] args) {
      // creating a linkedlist 
      LinkedList<String> input_list = new LinkedList<>();
      // creating an instance of ListIterator
      ListIterator<String> newList = input_list.listIterator();
      // adding elements to the list
      newList.add("Java");
      newList.add("Python");
      newList.add("Scala");
      newList.add("Shell");
      // printing the result
      System.out.println("The elements added to the list are: " + input_list);
   }
}

Output

The elements added to the list are: [Java, Python, Scala, Shell]

Example 4

In this example, we will use addFirst() and addLast() methods to insert elements at the first and last indices of the LinkedList.

import java.util.LinkedList;
public class Demo4 {
   public static void main(String[] args) {
      LinkedList<Integer> inputList = new LinkedList<>();
      // Adding elements in linkedlist
      inputList.add(8);
      inputList.add(4);
      inputList.add(1);
      inputList.add(0);
      System.out.println("Elements of the original Linkedlist : " + inputList);
      // adding elements to the first and last index
      inputList.addFirst(9);
      inputList.addLast(9);
      // to print the result
      System.out.println("After adding elements to the first and last index of Linkedlist : " + inputList);
   }
}

Output

Elements of the original Linkedlist : [8, 4, 1, 0]
After adding elements to the first and last index of Linkedlist : [9, 8, 4, 1, 0, 9]

Conclusion

We started this article by introducing the LinkedList which is a generic class of Java Collection Framework. In the next section, we have seen various built-in methods of this class that can be used to insert elements to the LinkedList collection. Those methods are: add(), addAll(), addFirst() and addLast().

Updated on: 10-Aug-2023

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements