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


The 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.


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


- 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


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 =;





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;

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

    public Employee(String name, int salary) { = 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 =

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


[John, Jane, Mike]


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.