Iterators and Cycles over Collections

In this exercise, you will learn how to work with **iterators** and **loops** in Java to iterate through collections such as **Lists**, **Sets**, and **Maps**. You will use control structures such as `for`, `while`, and `foreach` to access and manipulate elements within these collections, allowing you to handle large volumes of data efficiently and flexibly in your Java applications.

Topic

Data Structures and Collections

Java Exercise

In this exercise, you will create a Java program that uses **iterators** and **loops** to iterate through various collections such as **Lists**, **Sets**, and **Maps**. First, you will create a **List** collection, then use a **for** or **foreach** loop to iterate through the elements of the collection and display their contents. Next, you will implement a **while** loop to iterate over a **Set** and a **Map** using an **iterator**. This exercise will help you understand how to efficiently iterate through collections in Java and apply best practices when working with large amounts of data.

Instructions:

  1. Create a **List** collection with multiple elements and iterate over it using a **for** or **foreach** loop.
  2. Create a **Set** collection and iterate over it using a **while** loop with an **iterator**.
  3. Create a **Map** and iterate through its keys and values ​​using an **iterator**.
  4. Print the elements to the console to verify that the loops and iterators are working correctly.

This exercise will help you understand how **iterators** and **loops** allow you to iterate through and manipulate data in collections, improving the efficiency and flexibility of your Java code.


import java.util.*;

public class IteratorsLoopsCollections {
    public static void main(String[] args) {
        // Create a sample list
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        
        // Use a foreach loop to iterate over the list
        System.out.println("Iterating over the list:");
        for (String fruit : list) {
            System.out.println(fruit);
        }

        // Create a sample set
        Set<String> set = new HashSet<>();
        set.add("Red");
        set.add("Green");
        set.add("Blue");
        
        // Use an iterator to iterate over the set
        System.out.println("\nIterating over the set:");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // Create a sample map
        Map<String, Integer> map = new HashMap<>();
        map.put("Juan", 25);
        map.put("Ana", 30);
        map.put("Carlos", 22);
        
        // Use an iterator to iterate over the map
        System.out.println("\nIterating over the map:");
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, Integer>> mapIterator = entrySet.iterator();
        while (mapIterator.hasNext()) {
            Map.Entry<String, Integer> entry = mapIterator.next();
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

 Output:

Traversing the list:
Apple
Banana
Cherry

Traversing the set:
Blue
Green
Red

Traversing the map:
John: 25
Ana: 30
Charles: 22

This program demonstrates how to work with different types of collections in Java, such as **Lists**, **Sets**, and **Maps**, using `foreach` loops and iterators. The list is traversed with a `foreach` loop, the set is traversed using an iterator, and the map is traversed using an iterator over the set of entries. This approach allows for efficient data handling, iterating over collections and displaying their elements in a structured way.


 Share this JAVA exercise