Java Stream Map

--

In this article, we’ll go through the Java Stream map() method through detailed examples.

1. Java Stream Map Method Overview

The stream.map() method is used to transform an object or primitive into something else. The syntax of this method is the following:

Stream<Object_type_or_primitive> map(Function<? extends type_of_stream, Object_type_or_primitive variable>)

The function could have one of the following syntaxes:

1.1 One-Line Lambda

a_variable -> transformed_variable_to_another_object

The transformed_variable_to_another_object must be one line of code and it should transform the object into something else. This can be done either by calling a method of the a_variable or calling another method that accepts a_variable and returns something else.

1.2 Method Reference

Object_class_::a_method

In many cases, this can replace the previous syntax and the result will be the same.

1.3 Multiline Lambda

a_variable -> {
lines_of_code;
...
return transformed_variable_to_another_object;
}

The above will perform some operations before it returns the new object. Note that this cannot be replaced by method reference without calling an external method.

2. Java Stream Map Method Example With Lambda or Method Reference

Let’s say we have a list of article objects, the article record is the following:

private static record Article (int id, String title, String authorName){};

And we have a list of these articles:

Article forLoop = new Article(
1,
"For Loop Java Tutorial",
"Dimitris Tasios"
);

Article interfaceTutorial = new Article(
2,
"Java Interface Tutorial",
"Georgios - Nikolaos Palaiologopoulos"
);

Article generics = new Article(
3,
"Java Generics Tutorial",
"Ignis Dei"
);

Article mathRandom = new Article(
4,
"Math.random Java Tutorial",
"Georgios - Nikolaos Palaiologopoulos"
);

Now let’s say we’d like to have a list of the titles of the articles only. This can be achieved by mapping the article object to the string attribute authorName.

public static List<String> mapToTitle(List<Article> articles){

return articles.stream()
.map(Article::title)
.collect(Collectors.toList());
// return articles.stream()
// .map(article -> article.title())
// .collect(Collectors.toList());
}

The first return uses the Method Reference Operator (::); this is exactly like the second return which is commented but is more concise. However, as it has already been mentioned, it has its limitations as it cannot be applied to perform inline additional transformations without relying on a method to do it for you. For example, the following cannot be replaced with a method reference.

return articles.stream()
.map(article -> article.title().toUpperCase())
.collect(Collectors.toList());

3. Java Stream Map Method Example With Multiple Lines of Code

Furthermore, you can write multiple lines inside a lambda function in the map method; for example, we might like to print something before we transform the articles into article titles:

public static List<String> mapToTitleAndPrint(List<Article> articles){
return articles.stream()
.map(article -> {
System.out.println("This article is written by: " +article.authorName());
return article.title();
})
.collect(Collectors.toList());
}

The above method will print:

This article is written by:Dimitris Tasios
This article is written by:Georgios - Nikolaos Palaiologopoulos
This article is written by:Ignis Dei
This article is written by:Georgios - Nikolaos Palaiologopoulos

and transform the article object into the article title.

4. Conclusion

By now you should know how to use the stream.map() method of Java Stream API. You can find the source code on our GitHub page.

5. Sources

[1]: Stream (Java SE 12 & JDK 12 ) — Oracle Help Center

--

--

Georgios Nikolaos Palaiologopoulos

Experienced Java Developer | Focused on Backend Software Development with Java & Spring Boot | Technical Writer