Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the spectra-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home4/mahidhar/public_html/wp-includes/functions.php on line 6114
LinkedList - Java | tutorialQ

LinkedList – Java

Introduction to LinkedList

What is LinkedList?

LinkedList is a part of the Java Collections Framework. It provides a doubly-linked list data structure, which allows elements to be stored in a sequence with efficient insertions and deletions.

Why Use LinkedList?

  • Dynamic resizing
  • Efficient insertions and deletions (especially at the beginning and end)
  • Supports both list and deque operations
  • Allows null elements

Importing LinkedList

To use LinkedList, you need to import it from the java.util package.

Java
import java.util.LinkedList;

Creating a LinkedList

Syntax

Java
LinkedList<Type> linkedList = new LinkedList<>();

Example: Creating a LinkedList

Java
import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Create a LinkedList of String type
        LinkedList<String> fruits = new LinkedList<>();

        // Add elements to the LinkedList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Print the LinkedList
        System.out.println(fruits);  // Output: [Apple, Banana, Cherry]
    }
}

Common Methods of LinkedList

  1. Adding Elements
  2. Accessing Elements
  3. Modifying Elements
  4. Removing Elements
  5. Iterating through Elements
  6. Size of the LinkedList
  7. Checking if LinkedList Contains Element
  8. Clearing the LinkedList

Adding Elements

Example: Adding Elements

Java
import java.util.LinkedList;

public class AddingElements {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add(1, "Blueberry");  // Adding element at specific index

        System.out.println(fruits);  // Output: [Apple, Blueberry, Banana, Cherry]
    }
}

Accessing Elements

Example: Accessing Elements

Java
import java.util.LinkedList;

public class AccessingElements {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Accessing elements
        String firstFruit = fruits.get(0);  // Accessing first element
        String secondFruit = fruits.get(1);  // Accessing second element

        System.out.println("First Fruit: " + firstFruit);  // Output: First Fruit: Apple
        System.out.println("Second Fruit: " + secondFruit);  // Output: Second Fruit: Banana
    }
}

Modifying Elements

Example: Modifying Elements

Java
import java.util.LinkedList;

public class ModifyingElements {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Modifying elements
        fruits.set(1, "Blueberry");  // Changing the second element

        System.out.println(fruits);  // Output: [Apple, Blueberry, Cherry]
    }
}

Removing Elements

Example: Removing Elements

Java
import java.util.LinkedList;

public class RemovingElements {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Removing elements
        fruits.remove(1);  // Removing the second element
        fruits.remove("Cherry");  // Removing the element "Cherry"

        System.out.println(fruits);  // Output: [Apple]
    }
}

Iterating through Elements

Example: Iterating through Elements

Java
import java.util.LinkedList;

public class IteratingElements {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Iterating using for-each loop
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Iterating using for loop
        for (int i = 0; i < fruits.size(); i++) {
            System.out.println(fruits.get(i));
        }
    }
}

Size of the LinkedList

Example: Size of the LinkedList

Java
import java.util.LinkedList;

public class LinkedListSize {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Getting the size of the LinkedList
        int size = fruits.size();

        System.out.println("Size of the LinkedList: " + size);  // Output: Size of the LinkedList: 3
    }
}

Checking if LinkedList Contains Element

Example: Checking if LinkedList Contains Element

Java
import java.util.LinkedList;

public class ContainsElement {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Checking if LinkedList contains an element
        boolean containsApple = fruits.contains("Apple");
        boolean containsMango = fruits.contains("Mango");

        System.out.println("Contains Apple: " + containsApple);  // Output: Contains Apple: true
        System.out.println("Contains Mango: " + containsMango);  // Output: Contains Mango: false
    }
}

Clearing the LinkedList

Example: Clearing the LinkedList

Java
import java.util.LinkedList;

public class ClearingLinkedList {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Clearing the LinkedList
        fruits.clear();

        System.out.println("LinkedList after clearing: " + fruits);  // Output: LinkedList after clearing: []
    }
}

Combining Methods in a Program

Example: Combined LinkedList Operations

This example combines various operations like adding, accessing, modifying, removing, and iterating elements.

Java
import java.util.LinkedList;

public class CombinedLinkedListExample {
    public static void main(String[] args) {
        LinkedList<String> fruits = new LinkedList<>();

        // Adding elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Accessing elements
        System.out.println("First fruit: " + fruits.get(0));  // Output: First fruit: Apple

        // Modifying elements
        fruits.set(1, "Blueberry");
        System.out.println("Modified LinkedList: " + fruits);  // Output: Modified LinkedList: [Apple, Blueberry, Cherry]

        // Removing elements
        fruits.remove("Cherry");
        System.out.println("LinkedList after removal: " + fruits);  // Output: LinkedList after removal: [Apple, Blueberry]

        // Iterating elements
        System.out.println("Iterating using for-each loop:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Checking size
        System.out.println("Size of the LinkedList: " + fruits.size());  // Output: Size of the LinkedList: 2

        // Checking if LinkedList contains an element
        boolean containsApple = fruits.contains("Apple");
        System.out.println("Contains Apple: " + containsApple);  // Output: Contains Apple: true

        // Clearing the LinkedList
        fruits.clear();
        System.out.println("LinkedList after clearing: " + fruits);  // Output: LinkedList after clearing: []
    }
}

Conclusion

LinkedList in Java is a versatile and efficient class that provides a dynamic, doubly-linked list data structure. It offers methods for adding, accessing, modifying, removing, and iterating through elements. Understanding and utilizing these methods effectively can help you manage collections of data efficiently in your Java programs. Practice these operations to become proficient in using LinkedList.

Scroll to Top