1. Introduction to Iterators
Welcome back! Previously, we explored file operations, including creating, deleting, and reading files and directories. Now, we will dive into processing collections in Java, focusing on using Iterators and Streams to traverse, modify, and transform data effectively.
2. Working with collections
Java provides multiple ways to iterate through a collection. Each method has its advantages, depending on whether we need simple traversal or modifications.
Let’s start by creating a data collection. To keep it simple, we will use a `HashSet` as an example. Remember, it stores unique items without duplicates.
First, we import `HashSet` from `java.util`. We then create a `HashSet` object named `fruits`.
Then using `.add()`, we insert two elements, `Apple` and `Banana`, into the set.
Now we have a set containing 2 elements.
We will use this set throughout the video to demonstrate different iteration methods.
3. Use For-Each loop
Previously, we introduced the `for-each loop` when working with `ArrayList`. However, this loop is not limited to lists - it works with many types of data collections, including `Set`, `Map`, and more.
The syntax remains the same:
`for (type x : collection)`, which automatically processes each element.
Here, we use the same `fruits` set created earlier, which contains `Apple` and `Banana`.
In this example, `String` variable `fruit` takes on each value in `fruits` set, printing it one by one. This method is ideal for reading data efficiently without requiring explicit indexing or iterators.
4. Traversing a collection with Iterator
Now, let's explore `Iterator`, a more advanced way to iterate through a collection. Compared to the `For-Each` loop, `Iterator` offers more control, including manual navigation and safe element removal.
`Iterator` works with many collection types, such as `Set`, `List`, and `Queue`.
We start by importing the Iterator class from `java.util`, which is part of the standard library, so no installation is needed.
Using the `fruits` set from earlier, we call `.iterator()` to get an `Iterator` object.
`.hasNext()` checks if more elements are available and `.next()` retrieves each element.
The while-loop continues as long as `.hasNext()` is `true`, printing each item in the set one by one.
5. Removing elements with Iterator
What's more, unlike traditional loops, `Iterator` allows safe removal of elements during traversal.
We call `.iterator()` on the `fruits` set to retrieve an `Iterator` object.
Then, we iterate through the set using `.hasNext()` to check if more elements exist.
Inside the loop, `.next()` retrieves the next element. If the element starts with `A`, we call `.remove()` to delete it.
6. Removing elements with Iterator(Continued)
After the loop completes, `Apple` has been removed, and the updated set contains only `Banana`.
Using an `Iterator` ensures that removal operations are safe and prevents issues like `ConcurrentModificationException`, which occurs when modifying a list while using other iteration methods.
7. Summary
We have explored two key ways to iterate over data collections: `For-Each Loop` and `Iterator`.
The `For-Each Loop` is simple, but does not support modifications or work directly with `Map`.
`Iterator` provides more control, allowing removals and working with `Map` indirectly via `.keySet()`, `.values()`, or `.entrySet()`.
Understanding these differences helps us choose the best approach for our needs.
8. Let's practice!
Now it's time for you to practice!