Java Stream map() Example

1. Introduction

The map() method in Java Streams API is a powerful intermediate operation that transforms each element in the stream using a provided function. This method is essential for data transformation tasks, allowing developers to apply operations that can convert or modify elements efficiently within a stream. It's especially useful in scenarios involving data manipulation, such as converting objects, changing data types, or applying functions to elements.

Key Points

1. map() applies a function to each element of the stream and returns a stream consisting of the results.

2. It is used for transforming data without altering the size of the stream.

3. This method provides a way to perform operations like converting data types, updating values, or extracting information from complex objects.

2. Program Steps

1. Import necessary classes.

2. Create a stream of initial data.

3. Apply the map() method to transform the data.

4. Use a terminal operation to collect and display the results.

3. Code Program

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamMapExample {

    public static void main(String[] args) {
        // Step 2: Create a stream of initial data
        List<String> words = Arrays.asList("hello", "world", "stream", "java");

        // Step 3: Apply the map() method to transform each element to uppercase
        List<String> upperCaseWords = words.stream()
                                           .map(String::toUpperCase)
                                           .collect(Collectors.toList());

        // Step 4: Use forEach to output the results
        System.out.println("Transformed words:");
        upperCaseWords.forEach(System.out::println);
    }
}

Output:

Transformed words:
HELLO
WORLD
STREAM
JAVA

Explanation:

1. Initialization of Data: The program starts with a list of strings. This list acts as the input for the stream operations.

2. Transformation Using map(): The map(String::toUpperCase) method is called on the stream of strings. Here, String::toUpperCase is a method reference that serves as the function applied to each element. This transforms each string into its uppercase form.

3. Collecting Results: After transformation, the results are collected into a new list using Collectors.toList(). This step is crucial as map() is an intermediate operation and requires a terminal operation to trigger its execution.

4. Outputting the Results: The transformed list of words is printed to the console. This demonstrates how each original string has been converted to uppercase, showcasing the direct impact of the map() operation.

5. Utility and Use Cases: This example highlights the utility of map() in data processing tasks where modifications to data elements are required. Whether for formatting strings, converting object types, or applying mathematical functions to numbers, map() provides a streamlined approach to manipulating streams of data.


Comments