In this article, we will discuss how to iterate over a LinkedList
using an Iterator
in Java. We will cover the basics of using an Iterator
, providing examples to demonstrate its usage.
Table of Contents
- Introduction
- Using an Iterator to Iterate Over a
LinkedList
- Removing Elements During Iteration
- Complete Example
- Conclusion
Introduction
A LinkedList
in Java is a part of the Java Collections Framework and implements the List
and Deque
interfaces. It allows for efficient insertion and removal of elements. Using an Iterator
to traverse a LinkedList
provides a flexible way to access each element and optionally remove elements during iteration.
Using an Iterator to Iterate Over a LinkedList
The Iterator
interface provides methods to iterate over a collection. The hasNext()
method checks if there are more elements to iterate, and the next()
method returns the next element in the iteration.
Example
import java.util.Iterator;
import java.util.LinkedList;
public class IterateUsingIterator {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Creating an iterator
Iterator<String> iterator = fruits.iterator();
// Iterating over the LinkedList
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println("Fruit: " + fruit);
}
}
}
Output:
Fruit: Apple
Fruit: Banana
Fruit: Orange
Removing Elements During Iteration
The Iterator
interface also provides a remove()
method, which can be used to remove elements from the collection during iteration. This is particularly useful when you need to filter elements based on certain criteria.
Example
import java.util.Iterator;
import java.util.LinkedList;
public class RemoveDuringIteration {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Banana");
// Creating an iterator
Iterator<String> iterator = fruits.iterator();
// Iterating and removing "Banana" from the LinkedList
while (iterator.hasNext()) {
String fruit = iterator.next();
if ("Banana".equals(fruit)) {
iterator.remove();
}
}
// Printing the LinkedList after removal
System.out.println("LinkedList after removal: " + fruits);
}
}
Output:
LinkedList after removal: [Apple, Orange]
Complete Example
Here is a complete example that demonstrates both iterating over a LinkedList
and removing elements during iteration.
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListIteratorExample {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Banana");
// Creating an iterator
Iterator<String> iterator = fruits.iterator();
// Iterating over the LinkedList
System.out.println("Iterating over the LinkedList:");
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println("Fruit: " + fruit);
}
// Re-create iterator for removal example
iterator = fruits.iterator();
// Iterating and removing "Banana" from the LinkedList
while (iterator.hasNext()) {
String fruit = iterator.next();
if ("Banana".equals(fruit)) {
iterator.remove();
}
}
// Printing the LinkedList after removal
System.out.println("LinkedList after removal: " + fruits);
}
}
Output:
Iterating over the LinkedList:
Fruit: Apple
Fruit: Banana
Fruit: Orange
Fruit: Banana
LinkedList after removal: [Apple, Orange]
Conclusion
Using an Iterator
to traverse a LinkedList
in Java provides a flexible way to access and manipulate elements. This guide provided examples to demonstrate how to use an Iterator
to iterate over a LinkedList
and remove elements during iteration. By understanding these concepts, you can efficiently manage elements in your LinkedList
.
Comments
Post a Comment
Leave Comment