Java LinkedList Tutorial 🎯

beginner
6 min

Java LinkedList Tutorial 🎯

Welcome to the Java LinkedList Tutorial! In this lesson, we'll dive into understanding the LinkedList data structure, its implementation in Java, and how to use it effectively in your projects. 📝

What is a LinkedList?

A LinkedList is a linear data structure that consists of a collection of elements, where each element, called a Node, points to the next one in the sequence. This makes LinkedLists dynamic and efficient for inserting and deleting elements at any position. 💡

Why use a LinkedList?

LinkedLists are particularly useful when dealing with dynamic data structures that require frequent additions and removals. Unlike arrays, which have a fixed size, LinkedLists can expand and contract as needed, making them more flexible and efficient. ✅

Creating a LinkedList in Java

To create a LinkedList in Java, you'll use the LinkedList class from the java.util package. Here's a simple example of how to create and manipulate a LinkedList:

java
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> fruits = new LinkedList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); System.out.println(fruits); // [Apple, Banana, Orange] } }

In the above example, we create a LinkedList of Strings called fruits, add three elements, and print the LinkedList. 📝

Basic LinkedList Operations

Let's explore some basic operations you can perform on a LinkedList:

  1. Adding elements: You can add elements to the end of the LinkedList using the add() method, or at a specific index using the add(int index, E element) method.

  2. Removing elements: To remove the first occurrence of an element, use the remove() method. To remove an element at a specific index, use the remove(int index) method.

  3. Getting elements: To get an element at a specific index, use the get(int index) method.

  4. Checking the size: Use the size() method to find the number of elements in the LinkedList.

  5. Checking if empty: Use the isEmpty() method to check if the LinkedList is empty.

Advanced LinkedList Operations

Java LinkedLists also offer more advanced operations like inserting and deleting elements at arbitrary positions, which can be particularly useful in various real-world scenarios.

Here's an example demonstrating the insertion and deletion of elements at specific positions:

java
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> fruits = new LinkedList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); fruits.add(1, "Mango"); // Insert Mango at position 1 System.out.println(fruits); // [Apple, Mango, Banana, Orange] fruits.remove(2); // Remove the element at position 2 System.out.println(fruits); // [Apple, Mango, Banana] } }

In the above example, we insert a new element "Mango" at position 1 and then remove the element at position 2 (Orange). 📝

Quiz 🎯

Quick Quiz
Question 1 of 1

Which method in Java LinkedList is used to remove the first occurrence of an element?

That's it for our introductory lesson on Java LinkedLists! In the next lesson, we'll dive deeper into more advanced topics like LinkedList iterators and sorting algorithms using LinkedLists. Happy coding! 💡 🚀