Java Stream map()

In this guide, you will learn about the Stream map() method in Java programming and how to use it with an example.

1. Stream map() Method Overview

Definition:

The Stream.map() method is used to transform the elements of a stream using a given function. It returns a new stream consisting of the results after applying the provided function to each element of the original stream.

Syntax:

<R> Stream<R> map(Function<? super T, ? extends R> mapper)

Parameters:

- mapper: A non-interfering, stateless function that transforms an element of type T into an element of type R.

Key Points:

- The map() method does not modify the original stream but instead produces a new stream containing transformed elements.

- Like filter(), the map() operation is also an intermediate operation, allowing other stream operations to be chained after it.

- It retains the order of the elements based on their position in the original stream.

- The number of elements in the resulting stream is the same as the original stream unless combined with other operations like filter().

- If the stream is parallel, the map() operation will also work in parallel.

2. Stream map() Method - Simple Example

import java.util.stream.Stream;

public class StreamMapExample {
    public static void main(String[] args) {
        Stream<String> namesStream = Stream.of("Alice", "Bob", "Charlie", "Dave", "Eve");

        // Using map to transform names into their uppercase versions
        Stream<String> upperCaseNames = namesStream.map(String::toUpperCase);

        upperCaseNames.forEach(System.out::println);
    }
}

Output:

ALICE
BOB
CHARLIE
DAVE
EVE

Explanation:

In the provided example, we have a stream of names. We use the map() method to transform each name into its uppercase version. The resulting transformed stream is then printed using the forEach method, showcasing the names in all uppercase letters.

3. Stream map() Method - Real World Example

In a real-world scenario, you might often find yourself working with a list of objects and needing to perform some transformation on this list. Here’s an example where we have a list of Employee objects, and we want to obtain a list of their names:

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

class Employee {
    private final String name;
    private final int salary;

    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating a list of Employee objects
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee("John", 50000));
        employees.add(new Employee("Jane", 55000));
        employees.add(new Employee("Mike", 45000));

        // Using map() to obtain a list of employee names
        List<String> employeeNames = employees.stream()
                .map(Employee::getName)
                .collect(Collectors.toList());

        // Printing the list of employee names
        System.out.println(employeeNames); // Prints: [John, Jane, Mike]
    }
}

Output:

[John, Jane, Mike]

Explanation:

1. Create Employees: We create a List of Employee objects, each having a name and salary.

2. Stream and Map: We create a Stream from the employees list and use the map()` function to transform each Employee object into its name (a String).

3. Collect and Print: We collect the resulting Stream<String> into a List<String> and print this list, which contains the names of the employees.

Comments