LinkedList
in Java is a part of the Java Collections Framework and implements the List
interface. It provides a doubly-linked list data structure that allows for efficient insertion, removal, and traversal operations, especially when operating near the head or tail of the list. Here, we’ll explore LinkedList
through various examples to demonstrate its usage and benefits.
Basic Operations
Creating a LinkedList
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedList<String> animals = new LinkedList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println(animals); // Output: [Dog, Cat, Horse] } } |
Adding Elements
Elements can be added at the beginning, end, or any position within the list.
1 2 3 4 5 |
animals.addFirst("Elephant"); // Adds to the beginning animals.addLast("Zebra"); // Adds to the end animals.add(2, "Lion"); // Adds at index 2 System.out.println(animals); // Output: [Elephant, Dog, Lion, Cat, Horse, Zebra] |
Removing Elements
Elements can be removed from the beginning, end, or a specific position.
1 2 3 4 5 |
animals.removeFirst(); // Removes the first element animals.removeLast(); // Removes the last element animals.remove("Lion"); // Removes the first occurrence of "Lion" System.out.println(animals); // Output: [Dog, Cat, Horse] |
Iterating Over a LinkedList
Using for-loop
1 2 3 |
for (int i = 0; i < animals.size(); i++) { System.out.println(animals.get(i)); } |
Using for-each loop
1 2 3 |
for (String animal : animals) { System.out.println(animal); } |
Using Iterator
1 2 3 4 5 |
import java.util.Iterator; Iterator<String> iterator = animals.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } |
Other Useful Operations
Accessing Elements
1 2 3 |
String firstElement = animals.getFirst(); String lastElement = animals.getLast(); System.out.println("First: " + firstElement + ", Last: " + lastElement); // Output: First: Dog, Last: Horse |
Finding the Index of an Element
1 2 |
int index = animals.indexOf("Cat"); System.out.println("Index of Cat: " + index); // Output: Index of Cat: 1 |
Converting to an Array
1 2 |
String[] animalsArray = animals.toArray(new String[0]); System.out.println(Arrays.toString(animalsArray)); // Output: [Dog, Cat, Horse] |
LinkedList vs. ArrayList
While LinkedList
and ArrayList
both implement the List
interface, the choice between them depends on the specific needs of your application.
LinkedList
is generally preferred when:
- Frequent addition and removal of elements are required.
- You predominantly work with elements at the beginning or end of the list.
In contrast, ArrayList
may be more efficient for scenarios involving:
- Frequent access to list elements by their index.
- Less frequent addition/removal of elements.
Conclusion
LinkedList
in Java offers a flexible and efficient way to manage dynamic lists, with easy insertion and removal operations. Understanding how to utilize LinkedList
, along with its strengths and weaknesses compared to other list implementations like ArrayList
, is crucial for Java developers in crafting efficient and effective applications.