Java Program to Add elements to a LinkedList

Updated on December 13, 2024
Add elements to a linkedlist header image

Introduction

The LinkedList class in Java, part of the Java Collections Framework, provides a doubly linked list implementation of the List and Deque interfaces. This makes it an excellent choice for applications requiring frequent additions and deletions of list items. LinkedList performs faster than ArrayList when handling data dynamically due to its inherent structure, which allows elements to be added or removed without shifting the remainder of the data.

In this article, you will learn how to add elements to a LinkedList in Java through practical examples. Discover methods to insert data at specific positions, append elements to the end of the list, and even add all elements from another collection.

Basics of Adding Elements to LinkedList

Add Elements at the End

Adding elements to the end of a LinkedList utilizes the add(E e) method, which appends the specified element to the end of this list.

  1. Instantiate a LinkedList.

  2. Use the add(E e) method to add elements.

    java
    LinkedList<String> fruits = new LinkedList<>();
    fruits.add("Apple");
    fruits.add("Banana");
    fruits.add("Cherry");
    

    This block of code initializes a LinkedList of strings and adds three fruits to it. Each call to add places the new element at the end of the list.

Insert Elements at a Specific Position

Insert elements at a specified index within the list using add(int index, E element). This allows precise control over the element order.

  1. Start with an existing LinkedList.

  2. Use the add(int index, E element) to insert an item.

    java
    LinkedList<String> fruits = new LinkedList<>();
    fruits.add("Apple");
    fruits.add("Banana");
    fruits.add(1, "Mango");  // Inserts Mango before Banana
    

    Here, Mango is inserted into the LinkedList, positioned between Apple and Banana. Using the index parameter, you can specify exactly where in the list the element should appear.

Advanced Techniques

Add Elements from Another Collection

To add all elements from another collection, such as an ArrayList, to a LinkedList, you use the addAll(Collection<? extends E> c) method.

  1. Create a LinkedList and a secondary collection.

  2. Use addAll() to import all elements from another collection.

    java
    LinkedList<String> fruits = new LinkedList<>();
    ArrayList<String> moreFruits = new ArrayList<>();
    moreFruits.add("Kiwi");
    moreFruits.add("Orange");
    
    fruits.addAll(moreFruits);
    

    In the example, addAll() method is used to add all elements from moreFruits ArrayList into the fruits LinkedList. This is effective for merging lists.

Add Elements to the Beginning

Adding an element at the beginning of the list demonstrates the advantage of LinkedList over ArrayList in terms of performance.

  1. Initialize a LinkedList.

  2. Use addFirst(E e) to add an element at the start of the list.

    java
    LinkedList<String> fruits = new LinkedList<>();
    fruits.add("Banana");
    fruits.addFirst("Apple");  // Apple is now the first item
    

    This snipped places Apple as the first item of the list, pushing Banana to the second position.

Conclusion

Adding elements to a LinkedList in Java is a flexible process, adaptable to various use cases from appending elements to integrating entire collections. The class provides multiple methods tailored for different insertion strategies, which you can choose based on the specific needs of your program. Utilize these examples to effectively manage lists in your Java applications, benefiting from the quick insertion and deletion capabilities of the LinkedList data structure. As demonstrated, whether you are appending items to the end, inserting at a specific position, or merging from other collections, LinkedList handles it efficiently.