# map() method

In 
Published 2023-06-10

This tutorial explains how we can use map() method for Streams in Java.

Please take a look at the following example and read carefully the comments. The code is self-explanatory.

From the base application downloaded from Spring Initializr, I updated the main class as below:

DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		// Create the application context and start Spring Boot
		ApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);

		Integer[] numbers = { 1, 2, 3 };

		Stream.of(numbers)
				// We are using a simple lambda expression
				.map(x -> x*100)
				.forEach(p -> System.out.println(p));

		System.out.println("----------------------------");

		Stream.of(numbers)
				// We can define a more complex lambda expression
				.map(x -> {
					Integer x1 = x*100;
					x1 = x1 + 1;

					return x1;

				} )
				.forEach(p -> System.out.println(p));
	}
}

When I run this code I receive:

100
200
300
----------------------------
101
201
301

Process finished with exit code 0