Java Stream map()

Java 8 Stream.map() converts Stream<X> to Stream<Y>. For each object of type X, a new object of type Y is created and put in the new Stream.

1. Stream map() Method

1.1. Method Syntax

Stream map() method has following syntax.

<R> Stream<R> map(Function<? super T,? extends R> mapper)
  • R represents the element type of the new stream.
  • mapper is a non-interfering, stateless function to apply to each element which produces a stream of new values.
  • The method returns a new stream of objects of type R.

Stream interface has three more similar methods which produce IntStream, LongStream and DoubleStream respectively after the map operation.

If the streams created after map() operations are given return types then consider using these functions directly.

IntStream mapToInt(ToIntFunction<? super T> mapper)
LongStream mapToLong(ToLongFunction<? super T> mapper)
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

1.2. Description

  • The map() is an intermediate operation. It returns a new Stream as return value.
  • The map() operation takes a Function, which is called for each value in the input stream and produces one result value, which is sent to the output stream.
  • The mapper function used for transformation is a stateless function (does not store the information of previously processed objects) and returns only a single value.
  • The map() method is used when we want to convert a Stream of X to Stream of Y.
  • The mapped stream is closed after its contents have been placed into the new output stream.
  • map() operation does not flatten the stream as flatMap() operation does.

2. Stream map() Example

Example 1: Java program to convert a Stream of Strings to a Stream of Integers

In this example, we will convert a Stream<String> to Stream<Integer>. Here the mapper function Integer::valueOf() takes one string from the Stream at a time, and convert the String to an Integer.

It then put the Integer into another stream which is then collected using Collectors.toList().

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<String> listOfStrings = Arrays.asList("1", "2", "3", "4", "5");
		
		List<Integer> listOfIntegers = listOfStrings.stream()
						.map(Integer::valueOf)
						.collect(Collectors.toList());
		
		System.out.println(listOfIntegers);
	}
}

Program output.

[1, 2, 3, 4, 5]

Example 2: Java program to find all distinct salaries among all employees

Java example to find all possible distinct salaries for a List of employees.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class Main 
{
    public static void main(String[] args) 
    {
        List<Employee> employeesList = Arrays.asList(
                                            new Employee(1, "Alex", 100),
                                            new Employee(2, "Brian", 100),
                                            new Employee(3, "Charles", 200),
                                            new Employee(4, "David", 200),
                                            new Employee(5, "Edward", 300),
                                            new Employee(6, "Frank", 300)
                                        );
         
        List<Double> distinctSalaries = employeesList.stream()
                                .map( e -> e.getSalary() )
                                .distinct()
                                .collect(Collectors.toList());
 
        System.out.println(distinctSalaries);
    }
}

Program output.

[100.0, 200.0, 300.0]

Drop me your questions related to Stream map() method in Java Stream API.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.