- Introduction to LinkedList
- Importing LinkedList
- Creating a LinkedList
- Common Methods of LinkedList
- Adding Elements
- Accessing Elements
- Modifying Elements
- Removing Elements
- Iterating through Elements
- Size of the LinkedList
- Checking if LinkedList Contains Element
- Clearing the LinkedList
- Combining Methods in a Program
- Conclusion
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.
import java.util.LinkedList;
Creating a LinkedList
Syntax
LinkedList<Type> linkedList = new LinkedList<>();
Example: Creating a LinkedList
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
- Adding Elements
- Accessing Elements
- Modifying Elements
- Removing Elements
- Iterating through Elements
- Size of the LinkedList
- Checking if LinkedList Contains Element
- Clearing the LinkedList
Adding Elements
Example: Adding Elements
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
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
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
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
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
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
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
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.
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
.