1. Introduction

The TreeMap in Java is a Red-Black tree based Map that guarantees its elements to be in ascending key order. Like other Map types in Java, there are various methods to iterate through its elements. In this tutorial, we will explore different ways to iterate over a TreeMap.

2. Program Steps

1. Create a TreeMap and populate it with some key-value pairs.

2. Use the entrySet method combined with a for-each loop to iterate over the TreeMap.

3. Use the keySet method with a for-each loop to iterate over the keys and retrieve values.

4. Use the values method to iterate only over the values.

5. Use the forEach method to iterate over the TreeMap and display all entries.

3. Code Program

import java.util.TreeMap;

public class TreeMapIteration {

    public static void main(String[] args) {

        // Step 1: Create a TreeMap and populate it with some key-value pairs
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(1, "One");
        treeMap.put(3, "Three");
        treeMap.put(2, "Two");
        treeMap.put(4, "Four");

        // Step 2: Use entrySet with for-each loop to iterate
        System.out.println("Iterating using entrySet:");
        for (var entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }

        // Step 3: Use keySet with for-each loop to iterate
        System.out.println("\nIterating using keySet:");
        for (Integer key : treeMap.keySet()) {
            System.out.println(key + " => " + treeMap.get(key));
        }

        // Step 4: Use values method to iterate only over the values
        System.out.println("\nIterating over values:");
        for (String value : treeMap.values()) {
            System.out.println(value);
        }

        // Step 5: Use the forEach method for iteration
        System.out.println("\nIterating using forEach:");
        treeMap.forEach((key, value) -> System.out.println(key + " => " + value));
    }
}

Output:

Iterating using entrySet:
1 => One
2 => Two
3 => Three
4 => Four

Iterating using keySet:
1 => One
2 => Two
3 => Three
4 => Four

Iterating over values:
One
Two
Three
Four

Iterating using forEach:
1 => One
2 => Two
3 => Three
4 => Four

4. Step By Step Explanation

Step 1: We initialize a TreeMap and populate it with sample data.

Step 2: The entrySet method returns a set view of the mappings in the map. Using a for-each loop, we can easily iterate over each entry and get its key and value.

Step 3: The keySet method returns a set view of the keys. By iterating over these keys using a for-each loop, we can then fetch each corresponding value using the get method.

Step 4: The values method returns a collection view of the values present in the TreeMap. We can then use a for-each loop to directly iterate over these values.

Step 5: The forEach method provides another way to iterate over a TreeMap. It takes a lambda expression with two parameters (key and value) and allows us to process the entries in the map.