1. Introduction

In Java, Iterable is a root interface for collections, and Stream is a sequence of elements supporting sequential and parallel aggregate operations. Converting an Iterable to a Stream can be useful when we want to perform powerful aggregate operations on a sequence of elements, such as filtering, mapping, or reducing.

2. Program Steps

1. Import necessary libraries.

2. Create an Iterable.

3. Convert the Iterable to a Stream.

4. Perform operations on the Stream and display the output.

3. Code Program

// Step 1: Import necessary libraries
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class IterableToStreamExample {

    public static void main(String[] args) {

        // Step 2: Create an Iterable
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        // Step 3: Convert the Iterable to a Stream
        Stream<String> stream = StreamSupport.stream(list.spliterator(), false);

        // Step 4: Perform operations on the Stream and display the output
        stream.map(String::toUpperCase).forEach(System.out::println);
    }
}

Output:

APPLE
BANANA
CHERRY

4. Step By Step Explanation

Step 1: The required libraries are imported. These include classes for handling lists and streams.

Step 2: An Iterable is created in the form of an ArrayList named list and populated with string elements representing fruit names.

Step 3: The Iterable, list, is converted to a Stream using the StreamSupport.stream() method, where list.spliterator() is used to split the elements, and false indicates that it is not a parallel stream.

Step 4: Operations are performed on the Stream. Here, each string element in the stream is mapped to its uppercase form using the map() operation, and the results are printed to the console using the forEach() operation.